Javascript 删除谷歌地图圆圈/形状

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6058904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:04:01  来源:igfitidea点击:

Removing a Google maps Circle/shape

javascriptgoogle-maps

提问by Barrie Reader

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

我正在使用 google.maps.Circle() 方法创建一个 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);

回答by RedBlueThing

You need to call the setMapmethod on the Circle object to null:

您需要调用Circle 对象上的setMap方法为 null:

cityCircle.setMap(null);

回答by line break

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

要从地图中删除一个圆,请调用作为参数setMap()传递的方法null

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.

请注意,上述方法不会删除圆圈。它只是从地图上删除圆圈。相反,如果您希望删除圆圈,则应将其从地图中删除,然后将圆圈本身设置为null

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

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

回答by simo

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

您也需要删除事件侦听器,而不仅仅是隐藏圆圈,实际上circle.setMap(null)只会隐藏圆圈

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

回答by Kakera

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

谷歌地图 API 已更新。您现在可以直接使用以下代码:

circle.remove();