【发布时间】:2016-05-21 04:06:49
【问题描述】:
我正在使用以下函数打开一个设置 API 对话框,使用户能够打开位置服务,然后访问用户的当前位置:
public void onConnected(Bundle bundle) {
if(ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED) {
googleMapGlobal.setMyLocationEnabled(true);
Location mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (mCurrentLocation == null) {
// Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
//getApplicationContext().startActivity(intent);
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(VendorsOnMap.this, 1000);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
});
} else {
CameraPosition position = CameraPosition.builder()
.target(new LatLng(mCurrentLocation.getLatitude(),
mCurrentLocation.getLongitude()))
.zoom(15f)
.bearing(0.0f)
.tilt(0.0f)
.build();
googleMapGlobal.animateCamera(CameraUpdateFactory
.newCameraPosition(position), null);
LatLng latLng = new LatLng(31.220, 75.750);
Toast.makeText(getApplicationContext(), mCurrentLocation.getLatitude() + ", " + mCurrentLocation.getLongitude(), Toast.LENGTH_LONG).show();
googleMapGlobal.addMarker(new MarkerOptions().position(latLng).title("Marker here"));
}
}
else{
ActivityCompat.requestPermissions(getParent(),
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_ACCESS_FINE_LOCATION);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
final LocationSettingsStates states = LocationSettingsStates.fromIntent(intent);
if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
{
Toast.makeText(getApplicationContext(), resultCode + " " + Activity.RESULT_OK, Toast.LENGTH_SHORT).show();
switch (requestCode) {
case 1000:
switch (resultCode) {
case Activity.RESULT_OK:
googleMapGlobal.setMyLocationEnabled(true);
Location mCurrentLocation;
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
CameraPosition position = CameraPosition.builder()
.target(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()))
.zoom(15f)
.bearing(0.0f)
.tilt(0.0f)
.build();
googleMapGlobal.animateCamera(CameraUpdateFactory.newCameraPosition(position), null);
LatLng latLng = new LatLng(31.220, 75.750);
Toast.makeText(getApplicationContext(), mCurrentLocation.getLatitude() + ", " + mCurrentLocation.getLongitude(), Toast.LENGTH_LONG).show();
googleMapGlobal.addMarker(new MarkerOptions().position(latLng).title("Marker here"));
break;
case Activity.RESULT_CANCELED:
break;
default:
break;
}
break;
}
}
}
}
问题是当我通过打开位置来使用应用程序时,它运行得非常好,但是如果我从应用程序内的设置 API 对话框中打开位置,返回的位置为空。请帮忙。
【问题讨论】:
-
@DanielNugent 您提供的链接显示每次都返回 null 的位置,但在我的情况下,只有在应用程序内打开位置服务时才会发生这种情况(设置 API 对话框)。
-
您看到的是此用例的预期行为。在您启用服务和您尝试获取位置之间,没有足够的时间让其他应用请求位置。您永远不应该依赖 getLastLocation(),因为它可以而且会经常返回 null。
-
请务必在此处阅读答案/cmets:stackoverflow.com/questions/16830047/…
标签: android null location google-maps-api-2