deathdragon
deathdragon

Reputation: 51

Remove Marker in Google API V3

I would like to remove a marker in Google Maps, but I don´t understand this. Please help me.

My Validation.js:

function initialize() {
//geocodierungs Funktion damit Geocoding funktioniert
geocoder = new google.maps.Geocoder();  
var mapOptions = {
    zoom:5,
    center: new google.maps.LatLng(48.136607,11.577085),
    mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

var pos=new google.maps.LatLng(48.2,11);
var marker = new  google.maps.Marker({
    position:pos,
    map:map,
    title: 'test'
});}    setInterval(function(){         
       //$.post('nav_schnittstelle.php',{}).done(aktualisiereKartendaten);
       alert('test');
       pos.setMap(null); },10000);

How can I use setMap(Null);? I don´t understand this.

Upvotes: 5

Views: 17717

Answers (1)

Mr.Rebot
Mr.Rebot

Reputation: 6791

Try making a button and a listener to test your code.

To remove a marker from the map, call the setMap() method passing null as the argument.

marker.setMap(null);

Note that the above method does not delete the marker. It simply removes the marker from the map. If instead you wish to delete the marker, you should remove it from the map, and then set the marker itself to null.

Following the sample code in the document:

// Adds a marker to the map and push to the array.
function addMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: map
});
markers.push(marker);
}

// Sets the map on all markers in the array.
function setMapOnAll(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}

// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
setMapOnAll(null);
}

// Shows any markers currently in the array.
function showMarkers() {
setMapOnAll(map);
}

// Deletes all markers in the array by removing references to them.
function deleteMarkers() {
clearMarkers();
markers = [];
}

To remove a single marker, see the related SO question code snippet:

marker.addListener("dblclick", function() {
marker.setMap(null);
});

Hope this helps!

Upvotes: 7

Related Questions