如何删除Google map v2中的标记?

Rah*_*tte 2 android google-maps

我使用以下方法在地图上添加了标记,并保留了标记记录

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's 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

        }
        }


    }
Run Code Online (Sandbox Code Playgroud)

在自定义baseadapter中,我试图删除标记,但marker.remove()无法正常工作

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();




            }
        });
Run Code Online (Sandbox Code Playgroud)

请帮助如果有人经历过同样的事情.提前致谢

Rah*_*tte 10

我用Google搜索了很多,发现从地图中删除标记并不容易,每当你将标记添加到地图时都不要忘记保留其记录,比如将其添加到Map或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.
Run Code Online (Sandbox Code Playgroud)

所以Marker marker=your_google_map_obj.addMarker(new MarkerOptions())将这个标记对象添加到列表或地图markerArraylist.add(marker);然后很容易就可以从列表中提取标记 Marker marker=markerArraylist.get(index);,然后调用marker.remove();