0

Here's the problem:

I want to remove all map markers from the map when they go out of the viewport. I have the code that checks for their position, then runs setMap(null) on the marker. The marker disappears. I then set the marker itself to null to destroy it, per the Google Maps documentation.

The problem is that jquery-maps-ui's function for retrieving the map markers still includes the deleted markers!

function cleanUpMarkers(map) {
        console.log("$('#map_canvas').gmap('get', 'markers').length = " +  $('#map_canvas').gmap('get', 'markers').length);
        $.each($('#map_canvas').gmap('get', 'markers'), function (i, marker) {
            if (!map.getBounds().contains(marker.getPosition())) {
                console.log(marker.internalId);
                marker.setMap(null);
                marker = null;
                delete marker;


                //console.log(marker.internalId);

            }
        });
        console.log("$('#map_canvas').gmap('get', 'markers').length = " + $('#map_canvas').gmap('get', 'markers').length);

    }

How can I truly delete the marker so that $('#map_canvas').gmap('get', 'markers') doesn't return the deleted markers?

Sean Kendle
  • 3,538
  • 1
  • 27
  • 34
  • 1
    See http://stackoverflow.com/a/500617/2110460 for deleting an array element, which is what markers are essentially. – Rafe Mar 14 '13 at 19:53
  • Brilliant! Post that as an answer, rather than a comment so I can accept it! Thanks, Rafe, it works. I just had to change my for loop to use an index instead of using the jquery .each function. – Sean Kendle Mar 14 '13 at 20:35

0 Answers0