Javascript 删除 Google Maps API v3 中的标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7961522/
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-24 04:13:25  来源:igfitidea点击:

Removing a Marker in Google Maps API v3

javascriptgoogle-maps-api-3google-maps-markers

提问by Nathan Campos

I'm trying to remove a markerthat was initialized like this:

我正在尝试删除一个像这样初始化的标记

marker = new google.maps.Marker({
    position: latLng,
    map: map,
    draggable: true,
    animation: google.maps.Animation.DROP,
    title: 'Marker 1',
    icon: redPin
});

google.maps.event.addListener(marker, "click", function() {
    showMarkerDialog(marker.position, "marker");
});

google.maps.event.addListener(marker, "dblclick", function() {
    // Add a alert: Are you sure you want to remove this marker?

    map.removeOverlay(marker);
});

Everything works perfectly except that when I double click it to remove what I get on the Error Console is this:

一切正常,除了当我双击它以删除我在错误控制台上得到的内容时:

TypeError: Object # has no method 'removeOverlay'

类型错误:对象 # 没有方法“removeOverlay”

What am I doing wrong?

我究竟做错了什么?

回答by duncan

There is no removeOverlayfunction on the mapobject. Sounds like you've got only one marker, why use an array? Just change this:

地图对象上没有removeOverlay函数。听起来你只有一个标记,为什么要使用数组?只需改变这个:

google.maps.event.addListener(marker, "dblclick", function() {
    map.removeOverlay(marker);
});

to this:

对此:

marker.addListener("dblclick", function() {
    marker.setMap(null);
});