当 gps 关闭时,lastKnownLocation 总是返回 null
lastKnownLocation is always returning null when gps is off
我正在使用 Google 服务 API , LocationServices.FusedLocationApi 来查找用户的当前位置,然后更新位置。我已经在模拟器和实际设备上进行了测试,我发现如果我关闭 GPS LocationServices.FusedLocationApi.getLastLocation() 总是 returns null,但是如果我打开 GPS,我会得到一个有效值.这是我正在使用的代码:
private GoogleApiClient mGoogleApiClient;
private Location mLastKnownLocation;
mLastKnownLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastKnownLocation != null) {
Log.i(TAG, String.valueOf(mLastKnownLocation.getLatitude()));
Log.i(TAG, String.valueOf(mLastKnownLocation.getLongitude()));
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, createLocationRequest(), this);
我是不是漏掉了什么?
提前致谢。
我认为你应该首先检查你的设备是否有 GPS,方法是:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
然后使用LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient)
。
更多信息和代码请参考here。
我正在使用 Google 服务 API , LocationServices.FusedLocationApi 来查找用户的当前位置,然后更新位置。我已经在模拟器和实际设备上进行了测试,我发现如果我关闭 GPS LocationServices.FusedLocationApi.getLastLocation() 总是 returns null,但是如果我打开 GPS,我会得到一个有效值.这是我正在使用的代码:
private GoogleApiClient mGoogleApiClient;
private Location mLastKnownLocation;
mLastKnownLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastKnownLocation != null) {
Log.i(TAG, String.valueOf(mLastKnownLocation.getLatitude()));
Log.i(TAG, String.valueOf(mLastKnownLocation.getLongitude()));
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, createLocationRequest(), this);
我是不是漏掉了什么? 提前致谢。
我认为你应该首先检查你的设备是否有 GPS,方法是:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
然后使用LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient)
。
更多信息和代码请参考here。