shehzy
shehzy

Reputation: 2281

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?

Upvotes: 1

Views: 686

Answers (2)

Apoorv Singh
Apoorv Singh

Reputation: 1335

Add folowing line to the gradle-

compile 'com.google.android.gms:play-services:9.0.2'

in Code first add all your polylines in a list and then

map.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener() {
                                @Override
                                public void onPolylineClick(Polyline polyline) {




                                     if(polyline.getId().equals(polyLinesList.get(index).getId())){

                                          polyLines.List.get(index).remove
                                        }
                                    }
                                }});

Upvotes: 1

Simas
Simas

Reputation: 44118

First save all your polylines in a List like:

GoogleMap mMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

List<Polyline> mPolylines = new ArrayList<Polyline>();
// Add polylines to the map and the list
mPolylines.add(mMap.addPolyline(polyOpts));
...
mPolylines.add(mMap.addPolyline(polyOpts));

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() {
    @Override
    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) {
                    polyline.setVisible(false);
                    Log.e(TAG, "Found @ "+clickCoords.latitude+" "+clickCoords.longitude);
                    return;
                }
            }
        }
    }
});

To make this more precise:

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

Upvotes: 4

Related Questions