0
我想從谷歌地圖中刪除所有舊的標記,並添加新的標記,但setMap(null)不起作用,它添加新標記而不刪除舊標記。請看我的代碼marker.setMap(null);不起作用
var markers = data['map'];
if (marker != null) {
marker.setMap(null);
}
marker.setMap(null);
for (var i = 0; i < markers.length; i++) {
alert(1);
var data = markers[i];
var infoWindow = new google.maps.InfoWindow();
var img1 = data.imageUrl != '' ? '<img src= <?php echo $baseUrl; ?>/uploads/property/thumb/' + data.imageUrl + '>' : "";
var bed1 = data.beds != '' ? data.beds + ' beds' : "";
var bath1 = data.bath != '' ? data.bath + ' bath' : "";
var squarefeet1 = data.areaSqaureFeet != '' ? data.areaSqaureFeet + ' squarefeet' : "";
var contentString = '<div id="content">';
contentString += img1+ '<p>' + data.propertyAddress + ' </p><p> ' + bed1 + ' </p><p>' + bath1 + ' </p><p>' + squarefeet1 + ' </p>';
contentString += '</div>';
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: contentString
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(this.title);
infowindow.open(map, this);
});
}
}