0

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 ?

Community
  • 1
  • 1
mike_x_
  • 1,900
  • 3
  • 35
  • 69

1 Answers1

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);
    }
}
antonio
  • 18,044
  • 4
  • 45
  • 61