【问题标题】:locationManager.getLastKnownLocation() return nulllocationManager.getLastKnownLocation() 返回 null
【发布时间】:2014-07-17 09:35:30
【问题描述】:

我不明白为什么 locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 返回位置 null。我给予了所有许可,但它的 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 将无法工作。因此您还需要在这种情况下使用 NETWORK_PROVIDER 进行检查
  • @Biraj 我给了 NETWORK_PROVIDER 它仍然给出 null 直到某个时间之后。在继续之前如何确保不为空值

标签: android geolocation location latitude-longitude


【解决方案1】:

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

【讨论】:

  • 你说得对,建议看这个答案stackoverflow.com/questions/10524381/…
  • 这为我(应用程序开发人员)解决了这个问题,但是如果不告诉我的最终用户去 Google 地图进行 GPS 修复,我该如何做到这一点?
【解决方案2】:

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

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

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

【讨论】:

  • 所以你的答案是根本不使用getLastKnownLocation()?为什么?
【解决方案3】:

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

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      当您使用 GPS 作为提供商时,它会在 1 到 2 分钟内给出您的结果,因此您必须在获取位置停止时进行有争议的检查,并且网络提供商会在您请求时立即为您提供位置。因此,您不会在 GPS 提供商中获得即时位置纬度。

      GPS 仅在第一次时需要 1 到 2 次,然后它会为您提供您随时待命的位置...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-28
        相关资源
        最近更新 更多