Why getLastLocation() is always null?

时间:2016-03-02 10:57:12

标签: android android-maps

After starting a MapActivity, I check whether Location are enabled. If already enabled my code works fine but if initially location services are disabled I show up a dialog for activating location services. But after that I always get getLastLocation() method null.

onConnected callback:

public void onConnected(Bundle bundle) {
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(mLocationRequest);

        builder.setAlwaysShow(true);

        PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi
            .checkLocationSettings(mGoogleApiClient,
                    builder.build()
            );

        result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
            @Override
            public void onResult(LocationSettingsResult locationSettingsResult) {
                final Status status = locationSettingsResult.getStatus();
                switch (status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        Location lastLocation = LocationServices.FusedLocationApi
                           .getLastLocation(mGoogleApiClient);
                        Log.v("Last location:", lastLocation.toString());                            break;

                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        try {
                             status.startResolutionForResult(activity, ENABLE_LOCATION_REQUEST_CODE);
                         } catch (IntentSender.SendIntentException e) {
                             Log.e("Error: ", e.getMessage());
                         }
                        break;

                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        Log.e("Error", "Unavailable");
                        break;
                }
        }
    });

onActivityResult:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ENABLE_LOCATION_REQUEST_CODE) {
        switch (resultCode) {
            case Activity.RESULT_OK:
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED) {
                    Location lastLocation = LocationServices.FusedLocationApi
                            .getLastLocation(mGoogleApiClient);

                 // ********NullPointer Error here**********
                // lastLocation is always null
               // ******************************************

                    }
                break;
            case Activity.RESULT_CANCELED:
                Toast.makeText(this, "Location not enabled by user!", Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
    }
}

0 个答案:

没有答案
相关问题