locationManager.getLastKnownLocation() 返回 null。

19

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

你在设备上检查了吗? - Saqib
3
在办公室或建筑物内,GPS将无法工作。因此,在这种情况下,您还需要使用NETWORK_PROVIDER进行检查。 - Biraj Zalavadia
@Biraj 我已经使用了NETWORK_PROVIDER,但仍然返回null,直到一段时间后才会有值。在继续操作之前,我该如何确保它不为null? - Utsav Gupta
5个回答

22

我曾经遇到过完全相同的问题,那是因为我的设备没有存储最后已知的位置。我只需使用GPS在谷歌地图中标记我的位置,然后调用getLastKnownLocation()函数就可以得到返回值。


你是正确的,建议查看这个答案 https://dev59.com/xWkv5IYBdhLWcg3wiRWo - Udara Kasun
这对我作为应用程序开发者来说解决了问题,但我如何在不让终端用户去谷歌地图寻求 GPS 修复的情况下解决这个问题呢? - iSWORD

12

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

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

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


那么你的回答就是根本不使用getLastKnownLocation()吗?为什么? - Stealth Rabbi

4
根据文档,如果设备不知道上次的位置,则返回null。可能是GPS定位不到你的位置。无论如何,这需要大约一分钟或更长时间。所以试着去室外,在晴朗的天空下,远离高楼,等待GPS能够定位到你的位置。

3
我使用了这种方法来获取位置信息,我认为它会对你有帮助。
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");
    }
}

1
当您使用GPS作为提供者时,它会在1到2分钟内给出结果,因此当获取位置停止时,您必须持续检查。网络提供商会在您请求时立即提供位置,因此您不会在GPS提供者中获得即时位置经纬度。GPS仅在第一次使用时需要1到2分钟,之后它将在呼叫时提供位置...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接