Removing a Google maps Circle/shape

2020-05-23 02:38发布

I am creating a Circle using the google.maps.Circle() method. This all works fine and dandy, but how can I remove said circle?

My code:

var populationOptionsAgain = {
  strokeColor: "#c4c4c4",
  strokeOpacity: 0.35,
  strokeWeight: 0,
  fillColor: "#ffffff",
  fillOpacity: 0.35,
  map: map,
  center: results[0].geometry.location,
  radius: 40000
};
cityCircle = new google.maps.Circle(populationOptionsAgain);

4条回答
何必那么认真
2楼-- · 2020-05-23 03:16

You need to call the setMap method on the Circle object to null:

cityCircle.setMap(null);
查看更多
家丑人穷心不美
3楼-- · 2020-05-23 03:19

To remove a circle from the map, call the setMap() method passing null as the argument.

circle.setMap(null);

Note that the above method does not delete the circle. It simply removes the circle from the map. If instead you wish to delete the circle, you should remove it from the map, and then set the circle itself to null.

https://developers.google.com/maps/documentation/javascript/shapes#circle_remove

查看更多
小情绪 Triste *
4楼-- · 2020-05-23 03:20

Google Maps API has been updated. You can now directly use the following code :

circle.remove();
查看更多
Summer. ? 凉城
5楼-- · 2020-05-23 03:27

You need to remove events listeners too, not just hiding the circle, in-fact circle.setMap(null) will just hide the circle

function remove_circle(circle) {
    // remove event listers
    google.maps.event.clearListeners(circle, 'click_handler_name');
    google.maps.event.clearListeners(circle, 'drag_handler_name');
    circle.setRadius(0);
    // if polygon:
    // polygon_shape.setPath([]); 
    circle.setMap(null);
}
查看更多
登录 后发表回答