我在 android 中每隔 5 分钟 编写自己的后台位置更新。我想知道setInterval和setFastestInterval之间的区别
假设setFastestInterval(); 具有更高的请求Location 的优先级。对于您设置setFastestInterval(); 的任何应用程序,它将首先执行该应用程序(即使其他应用程序正在使用LocationServices)。
例如:如果 APP1 具有 setFastestInterval(1000 * 10) 并且 APP2 具有 setInterval(1000 * 10),则 两个 APPS 具有相同的请求间隔。但发出第一个请求的是 APP1。 (这是我的理解,答案可能不正确)
当我 setInterval 到 5 分钟 和 setFastestInterval 到 2 分钟。 location update 每 2 分钟 调用一次。
如果您将setFastestInterval() 与setInterval() 一起使用,则应用程序将尝试在setFastestInterval() 中指定的时间发出请求,这就是为什么您的应用程序每2 分钟发出一个请求 .
另外:只有当第一次更新的距离超过 20 米,第二次更新时,是否有内置功能来检查位置更新?
对于每 20 米发出请求,您可以创建一个LocationModel
public class LocationModel {
private double latitude;
private double longitude;
public LocationModel(){
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
}
在第一个请求中,您将lat 和long 设置为当前位置(使用getLastLocation();)
然后onLocationChanged()你从对象中获取数据并与new Current Location比较
float distanceInMeters = distFrom((float)locationObj.getLatitude(), (float)locationObj.getLongitude(), (float)mCurrentLocation.getLatitude(), (float)mCurrentLocation.getLongitude());
使用此功能也是SO用户的建议
public static float distFrom(float lat1, float lng1, float lat2, float lng2) {
double earthRadius = 6371; //kilometers
double dLat = Math.toRadians(lat2-lat1);
double dLng = Math.toRadians(lng2-lng1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
float dist = (float) (earthRadius * c);
return dist;
}