【问题标题】:How to remove the marker in Google map v2?如何删除谷歌地图 v2 中的标记?
【发布时间】:2013-12-09 17:09:03
【问题描述】:

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

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

        }
        }


    }

在自定义 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();




            }
        });

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

【问题讨论】:

  • 调用googleMap.clear()而不是删除单个标记是否有效?
  • @MaciejGórski 不,它不起作用,它会清除完整的地图。我自己得到了答案..你是同样的问题吗?
  • 没有。如果您找到了解决方案,请将其发布为答案并接受,这样寻求帮助的人就不会阅读它,而寻求帮助的人会向您学习。

标签: android google-maps


【解决方案1】:

我用谷歌搜索了很多,发现从地图上删除标记并不容易, 每当您将标记添加到地图时,请不要忘记保留其记录,例如将其添加到 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.

所以Marker marker=your_google_map_obj.addMarker(new MarkerOptions())将此标记对象添加到列表或映射markerArraylist.add(marker);然后您可以轻松地从列表中提取标记 Marker marker=markerArraylist.get(index); 然后拨打marker.remove();

【讨论】:

    【解决方案2】:
    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)));
    }
    

    【讨论】:

    • 是的,删除我的最后一个标记并创建当前标记
    • 它的工作尝试!我不明白为什么-1!它误导人!
    猜你喜欢
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 2020-04-02
    • 2011-01-28
    • 1970-01-01
    • 2018-10-13
    相关资源
    最近更新 更多