leo*_*ojg 5 android android-location
当我尝试在 android 中获取设备位置时遇到一个奇怪的问题。
有时它可以很好地获取当前位置,但有时它会不断返回旧位置。我所说的“旧”指的是几天前的情况,距离它应该所在的位置数十甚至数百公里。
我不知道我做错了什么,我尝试了几种不同的方法来获取位置,但似乎每种方法都有相同的问题。
最奇怪的是,它并不是在所有设备上都会发生……或者至少看起来是这样。三星 S3 和 S4 受影响最大,但在我的 Nexus 4 中从未发生过。
这是我的代码,也许你发现有问题:
public void startSearchingLocation() {
// Define a listener that responds to location updates
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location
// provider.
makeUseOfNewLocation(location);
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
String provider = getProvider();
/*I ignore the passive provider*/
if (provider == null
|| provider.contains(LocationManager.PASSIVE_PROVIDER)) {
this.validProvider = false;
} else {
this.validProvider = true;
}
if (validProvider) {
locationManager.requestLocationUpdates(provider, time, distance,
locationListener);
}
}
protected void makeUseOfNewLocation(Location location) {
JSONObject obj = new JSONObject();
try {
obj.put("latitude", location.getLatitude());
obj.put("longitude", location.getLongitude());
obj.put("provider", location.getProvider());
locationResult = obj;
} catch (Exception e) {
e.printStackTrace();
}
}
public JSONObject getLastKnownLocation() {
if (locationResult != null) {
return locationResult;
} else {
String provider = getProvider();
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
JSONObject obj = new JSONObject();
try {
obj.put("latitude", location.getLatitude());
obj.put("longitude", location.getLongitude());
obj.put("provider", location.getProvider());
return obj;
} catch (Exception e) {
e.printStackTrace();
}
}
}
return null;
}
public String getProvider() {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
String provider = locationManager.getBestProvider(criteria, true);
return provider;
}
Run Code Online (Sandbox Code Playgroud)
谢谢
注意:这并不是互联网连接不好。一些用户已经开始抱怨这个问题。而其他使用地理定位的应用程序运行良好。它一定是我的代码中的某些内容,但我不知道是什么。最奇怪的是,它似乎在一两周内运行良好,然后它开始同时在很多用户中发生
尝试用这种方式:
并使用 bestProvider
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location From GPS
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null)
{
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
Run Code Online (Sandbox Code Playgroud)