How to remove marker in Google map v2?

I added a marker to the map using the following method and also saved a marker record

public static void  showallLocations() 
    {
        ArrayList<LinkedHashMap<String, String>> listshow=latLngStoragepreference.getLatlng();
        markerlist=new ArrayList<Marker>();// to keep the marker record so that later we can delete
        if(listshow.size()>0)
        {
        for(int i=0;i<listshow.size();i++)
        {
        LinkedHashMap<String, String> Linkedhashmap=listshow.get(i);

        Set mapSet = (Set) Linkedhashmap.entrySet();
        //Create iterator on Set 
        Iterator mapIterator = mapSet.iterator();

        Map.Entry mapEntry = (Map.Entry) mapIterator.next();
        // getKey Method of HashMap access a key of map
        String keyValue = (String) mapEntry.getKey();
        //getValue method returns corresponding key value
        String value = (String) mapEntry.getValue();
        String[] parts=value.split("#");
        String Latitude=parts[0];
        String Longitude=parts[1];
        Double Latitudeconverted=Double.parseDouble(Latitude);
        Double Longitudeconverted=Double.parseDouble(Longitude);
        System.out.println(Latitudeconverted+""+Longitudeconverted);
        //show on map
        LatLng latLngs=new LatLng(Latitudeconverted, Longitudeconverted);

        Marker marker=map.addMarker(new MarkerOptions()
        .position(
                latLngs)
                .title(keyValue)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_navigate_to)));
        markerlist.add(marker);// keeping the record of marker object

        }
        }


    }

in the user base adapter, I tried to remove the marker, but it marker.remove()doesn’t work

holder.btnDeletelocation.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


        Marker marker=  MainActivity.markerlist.get(position);
        Log.d("MARKERlist before Remove", MainActivity.markerlist.toString());

        Log.d("MARKER Title",marker.getTitle());


        marker.remove();
        marker.setVisible(false);
        Log.d("MARKERlist after Remove", MainActivity.markerlist.toString());



            notifyDataSetChanged();




            }
        });

Please help if someone went through the same thing. thanks in advance

+2
source share
2 answers

I figured it out a lot and found that there is no easy way to remove a marker from a map; whenever you add a marker to a map, remember to save its record, for example, add it to a Map or ArrayList.

your_google_map_obj.addMarker(new MarkerOptions()) //this adds Marker on Google Map, you should know it always returns Marker object so that you can use it later especially for removal.

so Marker marker=your_google_map_obj.addMarker(new MarkerOptions())add this marker object to the list or map markerArraylist.add(marker);, then you can extract the marker from the list Marker marker=markerArraylist.get(index);, and then callmarker.remove();

+10
Marker currentMarker = null;
if (currentMarker!=null) {
    currentMarker.remove();
    currentMarker=null;
}

if (currentMarker==null) {
    currentMarker = mMap.addMarker(new MarkerOptions().position(arg0).
    icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
}
0

Source: https://habr.com/ru/post/1659911/


All Articles