locationManager.getLastKnownLocation()返回NULLlocationManager、getLastKnownLocation、NULL

2023-09-12 08:13:48 作者:咚咚丶尛破孩

我不明白为什么 locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 返回的位置空。我把所有的权限,但其reutning

 如果(isGPSEnabled){
            如果(位置== NULL){
                locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);
                Log.d(全球定位系统,GPS功能的);
                如果(locationManager!= NULL){
                    位置= locationManager
                            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    如果(位置!= NULL){
                        纬度= location.getLatitude();
                        经度= location.getLongitude();
                    }
                }
            }
        }
 

解决方案

我有此相同的问题。这是因为我的设备没有存储最后已知位置。我只是去上谷歌地图,并明确指出我的位置有GPS,那么这个值是返回getLastKnownLocation()

i dont understand why locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); return the location null. I gave all permission but its reutning 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();
                    }
                }
            }
        }
My Location Manager Lite

解决方案

I had this exact same problem. It was because my device was not storing a last known location. I simply went on to Google Maps and pinpointed my location with GPS, then a value was returned for getLastKnownLocation()

 
精彩推荐