Stephen J. Fuhry
unread,Aug 16, 2009, 9:34:40 AM8/16/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Google Maps JavaScript API v3
They should not be piling up, as you are effectively clearing any
previous value of prevMarker when you do `prevMarker = marker` and
`marker = new google.maps.Marker(...`
If you ever want to do anything more advanced such as clear the
markers that are not on the map as the map moves, you will have to use
hash tables. This is when you have to start worrying about deleting
values to keep them from piling up.
This code has been tested, and uses jQuery:
// this creates a marker and stores it in a hash table, allMarkers, so
i can perform actions on markers later without difficulty.
function createMarker(lat,lng,name,id) {
allMarkers[id] = {map:new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
map: map,
title: name,
clickable: true
})};
google.maps.event.addListener(allMarkers[id].map, 'click', function()
{
$.ajax({
type : "POST",
url : "data/getInfoWindow",
data : {id:id},
beforeSend: function() {
infowindow.set_content("loading...");
infowindow.open(map,allMarkers[id].map);
},
success : function(data){
infowindow.set_content(data); }
});
});
}
// this clears all markers on the map
function clearMarkers() {
$("div#numChurches").html("");
for (var i in allMarkers) {
allMarkers[i].map.set_map(null);
}
delete allMarkers;
}
// this deletes any markers not currently on the map
var bounds = map.get_bounds();
for (var i in allMarkers) {
if (!bounds.contains(allMarkers[i].map.get_position())) {
// must clear AND delete allMarkers[i], so the markers don't begin
to pile up
allMarkers[i].map.set_map(null);
delete allMarkers[i];
}
numMarkers++;
}