locationManager.getLastKnownLocation() 返回 null

时间:2023-03-23
本文介绍了locationManager.getLastKnownLocation() 返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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

推荐答案

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

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()

这篇关于locationManager.getLastKnownLocation() 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何在 iOS 上的变量中获取位置(纬度和经度值)? 下一篇:在应用启动期间获取当前位置

相关文章