Você tem duas opções, ambas envolvem manter uma referência ao marcador fora do
displayLocation
função:- use a referência para mover o marcador existente
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!'
});
}
}
- remova o marcador existente do mapa e crie um novo
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!'
});
}