PDA

View Full Version : how to REMOVE map markers?



spectrum24
28 Nov 2010, 3:14 PM
I've seen lots of examples here on adding map markers, but not how to remove them. I need to remove them all before each refresh. The Google API docs say this:


var markersArray = [];
// Deletes all markers in the array by removing references to them
function deleteOverlays() {
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
}
}

and add a markersArray.push(marker); to the addMarker function, which I did. But when I try deleteOverlays(), I get an error:

TypeError: Result of expression 'markersArray[i].setMap' [undefined] is not a function.

Ive tried variants of this with no success. Anyone know how to make this work within ST?

evant
28 Nov 2010, 3:29 PM
You'd probably be better off asking this in some Google Maps discussion group, it doesn't really have anything to do with Touch.

VinylFox
28 Nov 2010, 3:37 PM
The return from creating your marker (new google.maps.Marker) would need to be pushed into that markersArray - are you doing that?

spectrum24
28 Nov 2010, 3:48 PM
evant: It seems to have something to do with Touch, it works fine outside of this environment.

vinylFox: Yes, you probably missed it at the bottom of the post. I am doing a markersArray.push(marker) right after I create it.

Maybe something to do with the way Touch wraps the map object, and the setMap cant reference the map?

VinylFox
28 Nov 2010, 3:51 PM
I have no way of knowing what the variable "marker" contains, so no, I did not miss it.

evant
28 Nov 2010, 3:51 PM
The first thing to modify would be to iterate over the array properly: http://stackoverflow.com/questions/500504/javascript-for-in-with-arrays

spectrum24
28 Nov 2010, 5:02 PM
That did it -- I had not seen that method of doing it before, but figured it's the official Google way.. Maybe it's a safari thing. Anyway, changed to something more conventional and it worked. Thanks to both of you.