How can i show markers close to a route path in android maps?

北慕城南 提交于 2019-12-23 04:24:06

问题


I have an application with a map Activity. I also have put some markers on it.

I have found in this SO question how to draw a path between 2 points: Answer : Draw path between two points using Google Maps Android API v2

How can i show only those of my markers that are close to this path in a radius for example 500m ?


回答1:


You can use the PolyUtil.isLocationOnPath method from the Google Maps Android API Utility Library:

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)

You will need to iterate over your markers and make them visible or invisible depending on the returned value of PolyUtil.isLocationOnPath with your desired tolerance (500 in your example):

for(Marker marker : markers) {
    if (PolyUtil.isLocationOnPath(marker.getPosition(), yourRoutePath, false, 500)) {
        marker.setVisible(true);
    } else {
        marker.setVisible(false);
    }
}


来源:https://stackoverflow.com/questions/42855423/how-can-i-show-markers-close-to-a-route-path-in-android-maps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!