获取当前GPS位置android studio
问题描述:
我想写一个android应用程序,很明显,它使用用户的位置在地图活动上放置标记,或者在这种情况下打印经纬度。但是,我找不到适合我的教程或以前的问题。我正在寻找绝对最简单的方式,以位置对象或纬度和经度的形式使用gps来获取用户的当前位置。我当前的代码如下所示,直接从this tutorial:获取当前GPS位置android studio
public class MainActivity extends AppCompatActivity
implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
private TextView lat, lng;
private GoogleApiClient mGoogleApiClient;
private Location mCurrentLocation;
private boolean mRequestingLocationUpdates;
private LocationRequest mLocationRequest;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lat = (TextView) findViewById(R.id.lat);
lng = (TextView) findViewById(R.id.lng);
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onResume() {
super.onResume();
if (mGoogleApiClient.isConnected() && !mRequestingLocationUpdates) {
startLocationUpdates();
}
}
@Override
protected void onPause() {
super.onPause();
stopLocationUpdates();
}
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
}
@Override
public void onConnected(Bundle bundle) {
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mCurrentLocation != null) {
lat.setText(String.valueOf("Lat: " + mCurrentLocation.getLatitude()));
lng.setText(String.valueOf("Lng: " + mCurrentLocation.getLongitude()));
}
else
Log.i("Problem", "mCurrentLocation is null");
if (mRequestingLocationUpdates) {
startLocationUpdates();
}
}
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
updateUI();
}
private void updateUI() {
lat.setText(String.valueOf("Lat: " + mCurrentLocation.getLatitude()));
lng.setText(String.valueOf("Lng: " + mCurrentLocation.getLongitude()));
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
和我的AndroidManifest.xml是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.root.locationtest">
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="LocationTest"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我似乎总是得到日志消息“mCurrentLocation为空”,因此没有在应用更改。行mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
上还有一条警告,指出“呼叫需要可能被用户拒绝的权限”。任何帮助表示感谢,提前谢谢。
编辑: 我周围所有的线,或与“呼叫需要权限可以被用户拒绝”与if语句:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED)
从而解决了这个警告。但是,onConnect()方法中的权限被拒绝,并且mCurrentLocation仍然为空。
答
通话需要用户可能会拒绝的权限。这个错误是由于android的更高版本,在运行时需要获得用户的许可。只需在清单中声明不会有帮助。所以你需要添加代码的权限。
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
添加你的AndroidManifest.xml文件 – user2025187
只需按照我的教程在这个答案。这将帮助您获取当前位置,纬度,经度,并且实时更新当前的纬度和经度。 http://stackoverflow.com/a/42131361/4201474 – TranHieu