shehzy shehzy - 1 year ago 110
Android Question

Removing a polyline from multiple polylines in android

I have multiple polylines on google map and on user choice I have to remove one of them. how can I identify that which polyline user wants to remove?


First save all your polylines in a List like:

GoogleMap mMap = ((MapFragment)getFragmentManager().findFragmentById(;

List<Polyline> mPolylines = new ArrayList<Polyline>();
// Add polylines to the map and the list

Then register a map click listener. It will measure what is the smallest distance from any point of any polyline. Unfortunately this is the only way that I know of how this can be done, since there's no polyline click listener.

mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
    public void onMapClick(LatLng clickCoords) {
        for (PolylineOptions polyline : mPolylines) {
            for (LatLng polyCoords : polyline.getPoints()) {
                float[] results = new float[1];
                Location.distanceBetween(clickCoords.latitude, clickCoords.longitude,
                        polyCoords.latitude, polyCoords.longitude, results);

                if (results[0] < 100) {
                    Log.e(TAG, "Found @ "+clickCoords.latitude+" "+clickCoords.longitude);

To make this more precise:

  • Change with the minimum distance at different zoom levels.
  • Continue looping to find the polyline with the smallest distance.