【问题标题】:LocationManager returns null longitude with getLastKnownLocation in AndroidLocationManager 在 Android 中使用 getLastKnownLocation 返回空经度
【发布时间】:2016-09-21 21:57:51
【问题描述】:

我有一个应该返回用户位置的代码,但经度不断返回null,因此我的应用程序不断崩溃。如何防止这种情况发生?

//get the user longitude and latitude
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();

Geocoder geocoder;  //return meaningful data from user location
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
address = addresses.get(0).getAddressLine(0);
city = addresses.get(0).getLocality();

【问题讨论】:

  • 请发布您的错误堆栈跟踪
  • 双经度上的空指针异常 = location.getLongitude();
  • 您的location 为空,而不是您的经度。
  • lm.getLastKnownLocation 可能返回 null。
  • 如果它返回 null,我该如何处理这个位置

标签: java android android-location


【解决方案1】:

添加支票。

if (location != null) {
    latitude = location.getLatitude();
    longitude = location.getLongitude();
}

至于为什么它没有返回值,您需要添加一些日志记录。

要获得更完整的解决方案,请尝试以下操作:

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);
        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get destination from Network Provider
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                //  Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(
                            LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    // Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(
                                LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return location;
}

在这个实现中,这是一个类的方法,我从我的活动中调用它。以及在不再需要资源后释放资源所需的代码。

例如:

/**
 * Stop using GPS listener
 * Calling this function will stop using GPS in your app
 */
public void stopUsingGPS() {
    if (locationManager != null) {
        locationManager.removeUpdates(LocFinder.this);
    }
}

免责声明请注意,这只是所需代码的一部分,用于协助实施定位服务。资源在不使用时需要释放,位置服务的使用与项目的所有其他方面一样需要在整个应用程序的程序流程中进行管理。

查看Getting the Last Known LocationAndroid Location API - TutorialAndroid - Location Based Services 以更全面地实施定位服务。

【讨论】:

  • 我在这一行出现错误 locationManager = (LocationManager) mContext
  • 我在这里使用什么值 MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,
  • 如果您不实现onLocationChanged() 方法来接收位置更新,那么调用requestLocationUpdates() 有什么意义? GPS 在更新位置之前至少会有几秒钟的延迟,因此在此之后立即致电getLastKnownLocation() 不会为您提供最新的最新位置。不仅如此,位置更新不会被取消,因此 GPS 会保持活动状态,无论您以后是否再次调用此方法,它都会耗尽电池电量。这不是获取最新位置的好方法,不应该被赞成。
猜你喜欢
  • 2016-09-15
  • 1970-01-01
  • 1970-01-01
  • 2016-03-22
  • 2012-05-28
  • 2014-08-25
  • 1970-01-01
相关资源
最近更新 更多