【问题标题】:How can I remove specific group of marker from map?如何从地图中删除特定的标记组?
【发布时间】:2020-11-22 23:37:44
【问题描述】:

我已将不同组的手动创建的标记添加到地图中。现在我想从地图中删除特定的标记组。我试过使用 .remove();和 clear() 方法,没有用。这是我的代码。

 HashMap<Integer,Marker> aa = new HashMap<>();

 void addAtmkMarker(LatLng latLng, String title, boolean status) {
    Log.d(TAG, "moveCamera: Clearing Previous location");
    if (mMap != null) {

        Log.d(TAG, "moveCamera: Moving Camera to current location");
      

        MarkerOptions options = new MarkerOptions()
                .position(latLng)
                .title(title)
                .icon(status ? BitmapDescriptorFactory.fromResource(R.drawable.ic_green) : BitmapDescriptorFactory.fromResource(R.drawable.ic_red));
        mAtm = mMap.addMarker(options);
        i++;
        aa.put(i,mAtm); //created hashmap to store status of multiple markers. 



    }
}



 atm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(((CheckBox)view).isChecked()){
                    addAtmkMarker(new LatLng(latLng.latitude + Math.random() * (0 - 0.005), latLng.longitude + Math.random() * (0 - 0.005)), "SBI", true);
                    addAtmkMarker(new LatLng(latLng.latitude + Math.random() * (0.005 - 0), latLng.longitude + Math.random() * (0 - 0.005)), "BOI", true);
                    addAtmkMarker(new LatLng(latLng.latitude + Math.random() * (0.005 - 0), latLng.longitude + Math.random() * (0 - 0.005)), "SBI", false);
                    addAtmkMarker(new LatLng(latLng.latitude + Math.random() * (0.005 - 0), latLng.longitude + Math.random() * (0 - 0.005)), "SBI", true);

                }
                else{
                    for(int j=0;j<=4;j++) //logic implemented for removing marker but didn't worked.
                    {
                        aa.remove(j);
                    }


                }
            }
        });

【问题讨论】:

    标签: java android google-maps marker


    【解决方案1】:

    您必须使用 markerName 删除标记。 先获取名称,然后将其删除。

    aa.get(j).remove()
    

    【讨论】:

    • 调试您的代码并确保在删除操作之前获得标记标题。因为您需要标记名称来删除它。
    猜你喜欢
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    • 2014-08-18
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    相关资源
    最近更新 更多