admin管理员组

文章数量:1287517

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.

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.

Share Improve this question edited Sep 6, 2016 at 20:02 SurvivalMachine 8,35615 gold badges62 silver badges91 bronze badges asked Sep 4, 2016 at 16:31 deathdragondeathdragon 511 gold badge1 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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!

本文标签: javascriptRemove Marker in Google API V3Stack Overflow