当用户靠近标记时收到通知 - osmdroid
get notification when user comes near to a marker - osmdroid
我目前正在尝试构建一个应用程序,用户在靠近标记时会收到通知,或者在我的情况下,我正在使用 Osmdroid 的 ItemizedOverlay,我想知道是否有办法这也适用于数百个标记,而不会在几分钟内耗尽电池。
我看到了一些方法,但当你只有几个标记时,所有方法都是针对这种情况的。
如果有人能帮助我,我会很高兴。
如果标记具有静态位置,则无需重复加载它们。
如果您只想计算地球上两点之间的距离,并且您有经纬度坐标,那么就不需要任何 Google API 的地图或其他图书馆。这只会花费额外的开销和维护费用。只需像这样创建一个静态方法:
public static double getDistanceMeters(LatLng pt1, LatLng pt2){
double distance = 0d;
try{
double theta = pt1.longitude - pt2.longitude;
double dist = Math.sin(Math.toRadians(pt1.latitude)) * Math.sin(Math.toRadians(pt2.latitude))
+ Math.cos(Math.toRadians(pt1.latitude)) * Math.cos(Math.toRadians(pt2.latitude)) * Math.cos(Math.toRadians(theta));
dist = Math.acos(dist);
dist = Math.toDegrees(dist);
distance = dist * 60 * 1853.1596;
}
catch (Exception ex){
System.out.println(ex.getMessage());
}
return distance;
}
那么你可以这样做:
public static boolean checkDistanceIsClose(LatLng pt1, LatLng pt2, double distance){
boolean isInDistance = false;
try{
double calcDistance = getDistanceMeters(pt1, pt2)
if(distance <= calcDistance){
isInDistance = true;
}
}
catch (Exception ex){
System.out.println(ex.getMessage());
}
return isInDistance;
}
相同的算法适用于任何平台。只需将其翻译成适当的程序语言即可。
为了补充@Barns 的回答,2 条评论:
- 使用 osmdroid 时,您没有 LatLng,而是 GeoPoint,
并且您不需要编写自己的 getDistanceMeters,因为 GeoPoint 已经有了
这个方法:
GeoPoint.distanceToAsDouble(最终 IGeoPoint 其他)
我目前正在尝试构建一个应用程序,用户在靠近标记时会收到通知,或者在我的情况下,我正在使用 Osmdroid 的 ItemizedOverlay,我想知道是否有办法这也适用于数百个标记,而不会在几分钟内耗尽电池。 我看到了一些方法,但当你只有几个标记时,所有方法都是针对这种情况的。 如果有人能帮助我,我会很高兴。
如果标记具有静态位置,则无需重复加载它们。
如果您只想计算地球上两点之间的距离,并且您有经纬度坐标,那么就不需要任何 Google API 的地图或其他图书馆。这只会花费额外的开销和维护费用。只需像这样创建一个静态方法:
public static double getDistanceMeters(LatLng pt1, LatLng pt2){
double distance = 0d;
try{
double theta = pt1.longitude - pt2.longitude;
double dist = Math.sin(Math.toRadians(pt1.latitude)) * Math.sin(Math.toRadians(pt2.latitude))
+ Math.cos(Math.toRadians(pt1.latitude)) * Math.cos(Math.toRadians(pt2.latitude)) * Math.cos(Math.toRadians(theta));
dist = Math.acos(dist);
dist = Math.toDegrees(dist);
distance = dist * 60 * 1853.1596;
}
catch (Exception ex){
System.out.println(ex.getMessage());
}
return distance;
}
那么你可以这样做:
public static boolean checkDistanceIsClose(LatLng pt1, LatLng pt2, double distance){
boolean isInDistance = false;
try{
double calcDistance = getDistanceMeters(pt1, pt2)
if(distance <= calcDistance){
isInDistance = true;
}
}
catch (Exception ex){
System.out.println(ex.getMessage());
}
return isInDistance;
}
相同的算法适用于任何平台。只需将其翻译成适当的程序语言即可。
为了补充@Barns 的回答,2 条评论:
- 使用 osmdroid 时,您没有 LatLng,而是 GeoPoint,
并且您不需要编写自己的 getDistanceMeters,因为 GeoPoint 已经有了 这个方法:
GeoPoint.distanceToAsDouble(最终 IGeoPoint 其他)