当我试图在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;
}
谢谢
注意:网络连接不错。一些用户已经开始抱怨这个问题。而其他使用地理定位的应用程序运行良好。这肯定是我代码中的某些内容,但我不知道是什么。
最奇怪的是,它似乎可以工作一两周,然后它开始在许多用户中同时发生
最佳答案
我想说问题出在
criteria.setPowerRequirement(Criteria.POWER_LOW);
可能只是返回被动位置提供者。我要删除这一点,用户现在意识到这类应用消耗电池,所以这不会是一个强制的标准。
无论如何,我有一个更复杂的类来检索位置。让我解释一下。
首先,from here我使用这个伟大的函数来确定一个位置的读取是否优于当前位置。
/**
* @param location
* The new Location that you want to evaluate
* @param currentBestLocation
* The current Location fix, to which you want to compare the new
* one
*/
protected boolean isBetterLocation(Location location,
Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}
// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location, use
// the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must be
// worse
} else if (isSignificantlyOlder) {
return false;
}
// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation
.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and
// accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate
&& isFromSameProvider) {
return true;
}
return false;
}
/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
然后,我创建自己的实现locationlistener的类(活动或helper类,由您决定)。我只考虑GPS和网络提供商,但它很容易扩展
public class LocationObserver implements LocationListener {
LocationManager lm;
boolean gps_enabled = false;
boolean network_enabled = false, network_connected = false;
Context context;
Long timeout;
int minTime;
int minDistance;
Handler handler = new Handler();
private Location currentLocation = null;
public LocationObserver(Context pContext, int minTime,
int minDistance, long timeout) {
this.context = pContext;
this.timeout = timeout;
this.minDistance = minDistance;
this.minTime = minTime;
}
我创建一个方法,从我认为哪一个是最好的提供者开始,或者如果我可以监听多个提供者
public void start() {
if (lm == null)
lm = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
// exceptions will be thrown if provider is not permitted.
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
//dont worry yet
}
try {
network_enabled = lm
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
network_connected = activeNetwork != null
&& activeNetwork.isConnectedOrConnecting();
} catch (Exception ex) {
//dont wory yet
}
//now, lets see what happend,
//don't start listeners if no provider is enabled
if (!gps_enabled && !network_enabled) {
//nothing enabled, alert the user to enable any provide
//Settings.ACTION_LOCATION_SOURCE_SETTINGS;
}
else { // one provider is enabled, and the other one is not
if (gps_enabled) { // gps enabled, network disabled
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
minTime, minDistance, this);
//Consider asking the user to also enable network location ( Settings.ACTION_LOCATION_SOURCE_SETTINGS;=
} else { // gps disabled, network enabled
// check if actually there is a conection
if (network_connected) {
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
minTime, minDistance, this);
//OK, Network is working,
//consider asking the user to enable also gps (Settings.ACTION_LOCATION_SOURCE_SETTINGS;)
}
//if network is not enabled, having the network location enabled is useles
else {
//ask the user to connect to any network ( Settings.ACTION_WIRELESS_SETTINGS)
}
}
}
//retrieve the lastKnownLocation (we will see this function later) and call the callback to start using it
handler.postDelayed(new Runnable() {
@Override
public void run() {
Location l = getLastKnownLocation();
onLocationChanged(l);
}
}, timeout);
}
我们创建了自己的getLastKnownLocation(),它考虑所有启用的提供程序,并返回最佳位置
public Location getLastKnownLocation() {
Location net_loc = null, gps_loc = null;
if (gps_enabled)
gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (network_enabled)
net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// if there are both values use the best one
if (gps_loc != null && net_loc != null) {
if (isBetterLocation(gps_loc, net_loc))
return gps_loc;
else
return net_loc;
}
else if (gps_loc != null) {
return gps_loc;
} else if (net_loc != null) {
return net_loc;
} else
return null;
}
最后,我们编写onlocationchanged回调,提供者将使用任何更新调用它。因为所有的提供者都会调用它,可能即将到来的位置比我们已经拥有的位置更糟糕,所以我们也比较了它们
public synchronized void onLocationChanged(Location location) {
if (location == null || currentLocation == null
|| isBetterLocation(location, currentLocation)) {
currentLocation = location;
//do whatever you need with the new location
}
}
}
另外,我要做的是在第一次调用getLastKnowLocation之后继续使用计时器检查位置。如果特定时间过去了,我会重新检查所有内容,查看启用了哪些提供程序,或者再次要求用户启用禁用的提供程序。
另外,有些人喜欢直接依赖play services的位置api,在那里你不必担心提供者,也不需要关注底层位置技术的细节。https://developer.android.com/google/play-services/location.html