gpt4 book ai didi

java - LocationManager 在 Android 中使用 getLastKnownLocation 返回空经度

转载 作者:太空狗 更新时间:2023-10-29 16:00:15 24 4
gpt4 key购买 nike

我有一个应该返回用户位置的代码,但经度不断返回 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();

最佳答案

添加支票。

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

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

例如:
/**
* 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以便更全面地实现位置服务。

关于java - LocationManager 在 Android 中使用 getLastKnownLocation 返回空经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37417777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com