Du har to muligheder, begge involverer at beholde en reference til markøren uden for displayLocation
funktion:
- brug referencen til at flytte den eksisterende markør
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!'
});
}
}
- fjern den eksisterende markør fra kortet, og opret en ny
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!'
});
}