Anda memiliki dua opsi, keduanya melibatkan penyimpanan referensi ke penanda di luar displayLocation
fungsi:
- gunakan referensi untuk memindahkan penanda yang ada
var marker;
function displayLocation(location) {
console.log(location.lat)
var position = new google.maps.LatLng(parseFloat(location.lat), parseFloat(location.lng));
if (marker && marker.setPosition) {
// if the marker already exists, move it (set its position)
marker.setPosition(location);
} else {
// create a new marker, keeping a reference
marker = new google.maps.Marker({
map: map,
position: position,
title: 'test!'
});
}
}
- hapus penanda yang ada dari peta dan buat penanda baru
var marker;
function displayLocation(location) {
console.log(location.lat)
var position = new google.maps.LatLng(parseFloat(location.lat), parseFloat(location.lng));
if (marker && marker.setMap) {
// if the marker already exists, remove it from the map
marker.setMap(null);
}
// create a new marker, keeping a reference
marker = new google.maps.Marker({
map: map,
position: position,
title: 'test!'
});
}