我正在开发一个位置感知应用程序,当我请求位置更新时,我偶尔会得到一个非常旧的位置(好像它没有更新),或者我收到多个位置通知。我开始深入研究这个问题,发现这个博客描述了 ANDROIDS 位置监听器的工作原理。
简而言之,我的解释是,当 you 时requestLocationUpdates
,您不仅会获得一个位置对象,还会收到多个。因此,我开始尝试找出如何从多个位置对象中挑选出最佳位置对象,并在 Android文档中找到了一个算法(在“维护当前最佳估计”部分下)
我对如何将该部分中的代码块实现到我自己的应用程序中感到困惑。代码块接受两个参数,location
并currentbestlocation
比较它们。
- 您如何声明两个位置对象以供代码块进行比较?(代码示例或伪代码将不胜感激!)
locationlistener
我对提供多个位置对象的理解是否正确?- 如何在我的中实现 GOOGLES 代码?见下文:
我的代码如下:
public class MainActivity extends Activity {
LocationManager lm;
LocationListener ll;
private Location previousLocation;
public void onCreate(Context context, Intent intent) {
lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
ll = new myListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 1000, ll);
}
private class myListener implements LocationListener {
public void onLocationChanged(Location loc) {
if (previousLocation == null) {
previousLocation = loc;
} else {
if (isBetterLocation(loc, previousLocation)) {
//NOTIFICATION NEW LOCATION IS BETTER
} else {
//NOTIFICATION PREVIOUS LOCATION IS BETTER
}
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
//GOOGLE ANDROID DOCUMENTATION CODE FOR MAINTAINING CURRENT BEST ESTIMATE
private static final int TWO_MINUTES = 1000 * 60 * 2;
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;
}
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
}