0

我有一个小功能,可以在单击时添加标记和信息窗口。但是,当我单击新位置时,旧位置仍然存在。我希望它被新的取代。这是我的代码:

function MapInitialize() {

            var mapOptions = {
            zoom: 4,
            center: new google.maps.LatLng(43,-77),
            mapTypeId: google.maps.MapTypeId.ROADMAP
            }

            var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
        google.maps.event.addListener(map, 'click', function(event) {
            var marker = new google.maps.Marker({
                position: event.latLng,
                map: map
        })
        var infowindow = new google.maps.InfoWindow({
                content: 'Content Here'
            });
            infowindow.open(map,marker);
});
}

谢谢!

4

1 回答 1

1

在创建新标记之前删除旧标记:

var marker = null;
function MapInitialize() {

            var mapOptions = {
            zoom: 4,
            center: new google.maps.LatLng(43,-77),
            mapTypeId: google.maps.MapTypeId.ROADMAP
            }

            var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
        google.maps.event.addListener(map, 'click', function(event) {
            if (marker && marker.setMap) marker.setMap(null);
            marker = new google.maps.Marker({
                position: event.latLng,
                map: map
        })
        var infowindow = new google.maps.InfoWindow({
                content: 'Content Here'
            });
            infowindow.open(map,marker);
});
}
于 2013-02-15T16:43:28.667 回答