Android locationManager.getLastKnownLocation() 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23906926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:35:33  来源:igfitidea点击:

locationManager.getLastKnownLocation() return null

androidgeolocationlocationlatitude-longitude

提问by user3291590

i dont understand why locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);return the location null. I gave all permission but its reutning null.

我不明白为什么 locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);返回位置为空。我给了所有许可,但它的 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();
                    }
                }
            }
        }

采纳答案by 2bard

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

我有这个完全相同的问题。这是因为我的设备没有存储最后一个已知位置。我只是继续使用 Google 地图并使用 GPS 确定我的位置,然后为 getLastKnownLocation() 返回一个值

回答by aknopov

You can request location updates like this

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

mLocationManager.requestLocationUpdates(myProvider, 0, 0, locationListener);

then on the first callback in locationListener.onLocationChangedset your coordinates. Just don't forget to call mLocationManager.removeUpdates(locationListener)

然后在locationListener.onLocationChanged设置坐标的第一个回调中。只是不要忘记打电话mLocationManager.removeUpdates(locationListener)

回答by Farouk Touzi

Accoding to the documentation, it returns null, if the device is not aware of the last known location. Probably the GPS can not locate you. It takes about a minute or more, anyway. So try to go outside, under the clear sky, away from tall buildings, and wait until GPS can locate you.

根据文档,如果设备不知道最后一个已知位置,则返回 null。可能 GPS 无法定位您。无论如何,大约需要一分钟或更长时间。所以尽量到外面去,在晴朗的天空下,远离高楼,等待 GPS 定位到你。

回答by Pawan asati

I used this method for get location i think it will help you

我用这个方法来获取位置我认为它会帮助你

private void startReceivingLocationUpdates() {

    if (mLocationManager == null) {

        mLocationManager = (android.location.LocationManager)
                mContext.getSystemService(Context.LOCATION_SERVICE);

    }

    if (mLocationManager != null) {

        try {

            mLocationManager.requestLocationUpdates(

                    android.location.LocationManager.NETWORK_PROVIDER,
                    1000,
                    0F,
                    mLocationListeners[1]);

        } 
     catch (SecurityException ex) 
         {
            Log.i(TAG, "fail to request location update, ignore", ex);

        } 

       catch (IllegalArgumentException ex)
       {
            Log.d(TAG, "provider does not exist " + ex.getMessage());
        }

        try {

            mLocationManager.requestLocationUpdates(

                    android.location.LocationManager.GPS_PROVIDER,
                    1000,
                    0F,
                    mLocationListeners[0]);

            if (mListener != null) mListener.showGpsOnScreenIndicator(false);


        }
       catch (SecurityException ex) {

            Log.i(TAG, "fail to request location update, ignore", ex); } 

        catch (IllegalArgumentException ex) {

            Log.d(TAG, "provider does not exist " + ex.getMessage());  }

        Log.d(TAG, "startReceivingLocationUpdates");
    }
}

回答by Jayesh Khasatiya

When you use GPS as provider then it gives your result with in 1 to 2 mnts so you have to contentiously check for that when get location stop and Network Provider gives you immediate locationwhen you request. So you dont get immediate Location lat lon in GPS provider.

当您使用 GPS 作为提供者时,它会在 1 到 2 分钟内提供您的结果,因此您必须在获取位置停止时有争议地检查该结果,并且网络提供者在您请求时为您提供即时位置。因此,您无法在 GPS 提供商处获得即时的 Location lat lon。

GPS take 1 to 2 only first time then after it will give location you on call...

GPS 仅第一次需要 1 到 2,然后它会给你打电话的位置......