我如何在 android 地图中显示靠近路线路径的标记?
How can i show markers close to a route path in android maps?
我有一个带有地图的应用程序 Activity。我也做了一些标记。
我在这个 SO 问题中找到了如何在两点之间绘制路径:
Answer : Draw path between two points using Google Maps Android API v2
我如何才能仅显示半径(例如 500 米)内靠近此路径的那些标记?
您可以使用 Google Maps Android API Utility Library 中的 PolyUtil.isLocationOnPath
方法:
Computes whether the given point lies on or near a polyline, within a specified tolerance in meters. The polyline is composed of great circle segments if geodesic is true, and of Rhumb segments otherwise. The polyline is not closed -- the closing segment between the first point and the last point is not included.
public static boolean isLocationOnPath(LatLng point, List<LatLng> polyline, boolean geodesic, double tolerance)
您将需要遍历您的标记,并根据 PolyUtil.isLocationOnPath
的返回值和您想要的容差(在您的示例中为 500)使它们可见或不可见:
for(Marker marker : markers) {
if (PolyUtil.isLocationOnPath(marker.getPosition(), yourRoutePath, false, 500)) {
marker.setVisible(true);
} else {
marker.setVisible(false);
}
}
我有一个带有地图的应用程序 Activity。我也做了一些标记。
我在这个 SO 问题中找到了如何在两点之间绘制路径: Answer : Draw path between two points using Google Maps Android API v2
我如何才能仅显示半径(例如 500 米)内靠近此路径的那些标记?
您可以使用 Google Maps Android API Utility Library 中的 PolyUtil.isLocationOnPath
方法:
Computes whether the given point lies on or near a polyline, within a specified tolerance in meters. The polyline is composed of great circle segments if geodesic is true, and of Rhumb segments otherwise. The polyline is not closed -- the closing segment between the first point and the last point is not included.
public static boolean isLocationOnPath(LatLng point, List<LatLng> polyline, boolean geodesic, double tolerance)
您将需要遍历您的标记,并根据 PolyUtil.isLocationOnPath
的返回值和您想要的容差(在您的示例中为 500)使它们可见或不可见:
for(Marker marker : markers) {
if (PolyUtil.isLocationOnPath(marker.getPosition(), yourRoutePath, false, 500)) {
marker.setVisible(true);
} else {
marker.setVisible(false);
}
}