locationManager.getLastKnownLocation()返回null

use*_*590 15 android location geolocation latitude-longitude

我不明白为什么 locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);返回位置null.我给了所有的许可,但它的悔改null.

          if (isGPSEnabled) {
            if (location == null) {
                locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("GPS", "GPS Enabled");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

2ba*_*ard 15

我有同样的问题.这是因为我的设备没有存储最后一个已知位置.我只是转到Google地图并使用GPS精确定位我的位置,然后为getLastKnownLocation()返回了一个值


小智 12

您可以请求这样的位置更新

mLocationManager.requestLocationUpdates(myProvider, 0, 0, locationListener);
Run Code Online (Sandbox Code Playgroud)

然后在locationListener.onLocationChanged设置你的坐标的第一个回调.只是别忘了打电话mLocationManager.removeUpdates(locationListener)