【问题标题】:getLastLocation() returns null after turning on location from Settings Api Dialog从 Settings Api 对话框打开位置后,getLastLocation() 返回 null
【发布时间】: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


【解决方案1】:

开始工作了。我在理解getLastLocation() 的工作原理时犯了一个基本错误。它实际上是在寻找系统而不是应用程序接收到的位置。这是我修改后的代码。我现在使用设置 API 对话框让用户能够打开位置服务,但是 在此之前,我拨打了requestLocationUpdates() 的电话,以允许我的应用请求位置更新。 然后我在无限循环中调用getLastLocation(),直到它返回的值不为空。这样我就得到了设备的当前位置。

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;
            }
        }
    }

这里的mCurrentLocation是一个Location类型的全局变量。

【讨论】:

  • 主线程无限循环?不好。您现在的代码将冻结您的整个应用程序,如果您没有立即获得位置(这可能由于多种原因而发生),则抛出 ANR 正确的方法是在 onResult() 中调用 requestLocationUpdates(),然后获取来自onLocationChanged() 的位置。可以调用getLastLocation()一次,如果位置不为空,则使用它。否则,请致电requestLocationUpdates(),并在'onLocationChanged()'中获得一个位置后处理该位置
  • 是的!对不起!我不喜欢在 UI 线程中放置 while 循环。我通过在 AsyncTask 中放置无限循环来完成相同的任务,它不会冻结 UI 并且也可以完成我的工作。
猜你喜欢
  • 2014-09-20
  • 2016-09-30
  • 2013-07-27
  • 2013-06-24
  • 1970-01-01
  • 2015-08-15
  • 1970-01-01
  • 2017-09-13
相关资源
最近更新 更多