我有一个小功能,可以在点击时添加标记和信息窗口。但是,当我点击新位置时,旧位置仍然存在。我希望它被新的替换。这是我的代码:
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);
});
}
谢谢!
答案 0 :(得分: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);
});
}