Android getLastLocation()在从设置Api对话框打开位置后返回null

Android getLastLocation()在从设置Api对话框打开位置后返回null,android,null,location,google-maps-api-2,Android,Null,Location,Google Maps Api 2,我使用以下功能打开设置API对话框,使用户能够打开位置服务,然后访问用户的当前位置: public void onConnected(Bundle bundle) { if(ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED) { g

我使用以下功能打开设置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;
            }
        }
    }
}
public void未连接(捆绑){
if(ContextCompat.checkSelfPermission(getApplicationContext(),android.Manifest.permission.ACCESS\u FINE\u LOCATION)==PackageManager.permission\u已授予){
googleMapGlobal.setMyLocationEnabled(true);
Location mCurrentLocation=LocationServices.FusedLocationApi.getLastLocation(GoogleAppClient);
if(mCurrentLocation==null){
//意向意向=新意向(设置、动作、位置、来源、设置);
//getApplicationContext().startActivity(intent);
LocationRequest LocationRequest=LocationRequest.create();
locationRequest.setPriority(locationRequest.PRIORITY\u高精度);
位置请求设置间隔(30*1000);
locationRequest.SetFastTestInterval(5*1000);
LocationSettingsRequest.Builder=新建LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
Pendingreult结果=
LocationServices.SettingsApi.checkLocationSettings(GoogleAppClient,builder.build());
result.setResultCallback(新的ResultCallback(){
@凌驾
公共void onResult(位置设置结果){
最终状态状态=result.getStatus();
最终位置SettingsStates状态=结果。getLocationSettingsStates();
开关(status.getStatusCode()){
案例位置设置StatusCodes.SUCCESS:
//满足所有位置设置。客户端可以初始化位置
//请求在这里。
打破
案例位置设置StatusCodes.RESOLUTION_要求:
//不满足位置设置。但可以通过显示用户来修复
//对话。
试一试{
//通过调用startResolutionForResult()显示对话框,
//并在onActivityResult()中检查结果。
status.StartResult解决方案(VendorsOnMap.this,1000);
}catch(IntentSender.sendtintentexe){
//忽略错误。
}
打破
案例位置设置StatusCodes.SETTINGS\u CHANGE\u不可用:
//位置设置不满意。但是,我们无法修复此问题
//设置,这样我们就不会显示对话框。
打破
}
}
});
}否则{
CameraPosition位置=CameraPosition.builder()
.target(新LatLng(mCurrentLocation.getLatitude(),
mCurrentLocation.getLongitude())
.zoom(15f)
.轴承(0.0f)
.倾斜(0.0f)
.build();
googleMapGlobal.animateCamera(CameraUpdateFactory
.newCameraPosition(位置),空);
LatLng LatLng=新LatLng(31.220,75.750);
Toast.makeText(getApplicationContext(),mCurrentLocation.getLatitude()+”,“+mCurrentLocation.getLatitude(),Toast.LENGTH_LONG).show();
googleMapGlobal.addMarker(新的MarkerOptions().position(latLng.title)(“此处的标记”);
}
}
否则{
ActivityCompat.requestPermissions(getParent(),
新字符串[]{android.Manifest.permission.ACCESS_FINE_LOCATION},MY_permission_ACCESS_FINE_LOCATION});
}
}
ActivityResult上受保护的void(int-requestCode、int-resultCode、Intent-Intent){
最终位置设置状态=位置设置状态。来自意图(意图);
if(ContextCompat.checkSelfPermission(getApplicationContext(),android.Manifest.permission.ACCESS\u FINE\u LOCATION)==PackageManager.permission\u已授予){
{
Toast.makeText(getApplicationContext(),resultCode+“”+Activity.RESULT\u确定,Toast.LENGTH\u SHORT).show();
开关(请求代码){
案例1000:
开关(结果代码){
案例活动。结果\u正常:
googleMapGlobal.setMyLocationEnabled(true);
位置mCurrentLocation;
mCurrentLocation=LocationServices.FusedLocationApi.getLastLocation(GoogleAppClient);
CameraPosition位置=CameraPosition.builder()
.target(新LatLng(mCurrentLocation.getLatitude(),mCurrentLocation.getLongitude())
.zoom(15f)
.轴承(0.0f)
.倾斜(0.0f)
.build();
googleMapGlobal.animateCamera(CameraUpdateFactory.newCameraPosition(position),null);
LatLng LatLng=新LatLng(31.220,75.750);
Toast.makeText(getApplicationContext(),mCurrentLocation.getLatitude()+”,“+mCurrentLocation.getLatitude(),Toast.LENGTH_LONG).show();
googleMapGlobal.addMarker(新的MarkerOptions().position(latLng.title)(“此处的标记”);
打破
案例活动.RESULT\u已取消:
打破
违约:
打破
}
打破
}
}
public void onConnected(Bundle bundle) {

    if(ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED) {
        googleMapGlobal.setMyLocationEnabled(true);
        locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(0);
        locationRequest.setFastestInterval(5 * 1000);
        locationRequest.setNumUpdates(5);
        LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
           // Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            //getApplicationContext().startActivity(intent);
            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.
                            while(mCurrentLocation==null) {
                                if(ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED)
                                    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 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{
        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:
                            while (mCurrentLocation==null){
                                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;
            }
        }
    }