1

我正在使用下面的代码将标记添加到地图它还有一个侦听器来从地图中删除标记,但我无法弄清楚如何在从地图中删除标记的同时从标记数组中删除标记.

有人可以告诉我如何实现这一目标。

function createRedMarker(Lat, Lang, html, atitle) {
    var latlng = new google.maps.LatLng(Lat, Lang);
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: atitle,
        icon: redmarker,
        shadow: mshadow
    });

    google.maps.event.addListener(marker, "rightclick", function() {
        if (confirm("Deselect " + marker.title + " and remove from Map?")) {
            marker.setMap(null);
            window.status = "Deselected>" + marker.title + "<";
        }
    });

    markersArray.push(marker);
}
4

1 回答 1

0

请看一下这个解决方案。希望这会有所帮助。

function createRedMarker(Lat, Lang, html, atitle) {
    var latlng = new google.maps.LatLng(Lat, Lang);
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: atitle,
        icon: redmarker,
        shadow: mshadow
    });

    google.maps.event.addListener(marker, "rightclick", function() {
        if (confirm("Deselect " + marker.title + " and remove from Map?")) {
            marker.setMap(null);
            for (var i = 0; i < markers.length; i++) {
                if (markers[i].getPosition().equals(marker.getPosition())) {
                    var index = markers.indexOf(marker);
                    markers.splice(index, 1);
                    break;
                }
            }
            window.status = "Deselected>" + marker.title + "<";
        }
    });

    markersArray.push(marker);
}
于 2013-06-27T21:49:11.663 回答