How can I remove marker with bonded circle from the map?

岁酱吖の 提交于 2021-01-27 22:24:25

问题


I did bind the circle with marker to make view like:

enter image description here

But when I try to remove marker from the map, the circle still exists. Hmm,

How can I remove marker with circle?

Relevant code:

 function removeMarker(){
    if(selectedMarker)
       selectedMarker.setMap(null);      
}

....

function createCircle()
{
                var circle = {
                strokeColor: "#006DFC",
                strokeOpacity: 0.4,
                strokeWeight: 2,
                fillColor: "#006DFC",
                fillOpacity: 0.15,
                map: mapA,
                center: selectedMarker.getPosition(),
                radius: 50 // in meters
            };
            var cityCircle = new google.maps.Circle(circle);    


            cityCircle.bindTo('center', selectedMarker, 'position');
}

Any suggestions would be appreciated.

Thanks,


回答1:


If you want to remove the Circle when you remove the marker, you need to remove it also.

 cityCircle.setMap(null);

You will need to keep a reference to the circle to do that (not tested):

function removeMarker(){
  if(selectedMarker)
    selectedMarker.setMap(null); 
    selectedMarker._mycityCircle.unbindAll();
    selectedMarker._mycityCircle.setMap(null);  
}

....

function createCircle()
{
  var circle = {
    strokeColor: "#006DFC",
    strokeOpacity: 0.4,
    strokeWeight: 2,
    fillColor: "#006DFC",
    fillOpacity: 0.15,
    map: mapA,
    center: selectedMarker.getPosition(),
    radius: 50 // in meters
  };

  cityCircle.bindTo('center', selectedMarker, 'position');
  selectedMarker._mycityCircle = cityCircle;
}


来源:https://stackoverflow.com/questions/14006156/how-can-i-remove-marker-with-bonded-circle-from-the-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!