I want to change a Marker's map on-the-fly. Specifically, I have three collections of different markers, so I'm building them all with an undefined map, then trying to set their map property when I switch between the collections. It isn't working- the markers only display when passed a map in the constructor.
//Build a new marker with no map
var marker = new google.maps.Marker({
position: pos,
title: el.name + " #" + el.location_id,
map: null
});
markers[el.location_id] = marker;
//Later, 开发者_JAVA百科when displaying
$.each(markers, function(i, marker){
marker.map = map;
});
//Marker doesn't show up.
Further, when I create a marker, then remove its map, it still displays:
var marker = new google.maps.Marker({
position: pos,
title: el.name + " #" + el.location_id,
map: map
});
marker.map = null;
//Marker is still on the map...
You want to use the Marker object's map setter method, i.e.:
$.each(markers, function(i, marker){
marker.setMap(map);
});
Setting the map to null should remove it from any map on which the Marker object is displayed.
See the methods section under the v3 documentation.
You need to use setMap() method of the Marker so in your case
marker.setMap(map)
To remove a marker:
marker.setMap(null)
精彩评论