Remove markers out of viewport

2.3k Views Asked by At

I have to manage a map of about 80.000 markers concentrated in France. To do that, I decided to get the bounds of the viewport and call a dynamic-JSON (with PHP) which contains the markers inside the viewport. And this on the "idle" event.

I faced a problem with this solution. Indeed, the markers which already exist was re-plotted (at the same position), which consequently weigh the map for nothing...

To solve it, the markers list before and after the JSON query are compared (thanks to jQuery), in order to plot only the new markers. And it works!

Now, I would want to remove the markers which are not currently shown on the map. Or a list of markers (I get it thanks to jQuery) designated by an ID which is also the title of the marker. So, how can a delete markers like that ? I specify that I am using MarkerManager.

Otherwise, you guess that if I do not remove these markers, they will be re-plotted in some cases... For example, you are viewing the city A, you move the map to view the city B, and you get back to the city A...

Here is the code:

var map;
var mgr;
var markers = [];

function initialize(){

    var mapOptions = {
        zoom: 6,
        center: new google.maps.LatLng(46.679594, 2.109375)
    };
    map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

    var mgrOptions = { borderPadding: 50, maxZoom: 15, trackMarkers: false };

    mgr = new MarkerManager(map, mgrOptions);

    google.maps.event.addListener(map, 'idle', function() {
        mapEvent();
    });

}

function mapEvent(){
    if( map.getZoom() >= 8 ){
        var bounds = map.getBounds();
        getSupports(bounds.getNorthEast(), bounds.getSouthWest());
    } else {
        // Todo
    }
}


var markerslistID = new Array();
var markerslistData = {};

function getSupports(ne, sw){

    newMarkerslistID = new Array();
    newMarkerslistData = {};

    // Getting the markers of the current view
    $.getJSON('./markerslist.php?nelat='+ne.lat()+'&nelng='+ne.lng()+'&swlat='+sw.lat()+'&swlng='+sw.lng(), function(data) {

        for (var i = 0; i < data.points.length; i++) {
            var val = data.points[i];

            newMarkerslistID.push(val.id);
            newMarkerslistData[val.id] = new Array(val.lat, val.lng, val.icon);
        }

        // List of New Markers TO PLOT
        var diffNewMarkers = $(newMarkerslistID).not(markerslistID).get();
        // List of Old markers TO REMOVE
        var diffOldMarkers = $(markerslistID).not(newMarkerslistID).get();

        // Plotting the NEW MARKERS
        for( var i = 0; i < diffNewMarkers.length; i++ ){
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(newMarkerslistData[diffNewMarkers[i]][0], newMarkerslistData[diffNewMarkers[i]][1]),
                title : diffNewMarkers[i],
                icon : './images/'+newMarkerslistData[diffNewMarkers[i]][2]+'.png'
            });

            mgr.addMarker(marker, 0);

        }

        /*****************************************
        HERE WE HAVE TO REMOVE
        THE MARKERS CONTAINED IN diffOldMarkers
        *****************************************/

        mgr.refresh();

        // Switching the new list to the old, for the next event 
        markerslistID = newMarkerslistID;
        markerslistData = newMarkerslistData;

    });
}

Thank you for your help.

1

There are 1 best solutions below

1
eyecatchUp On

A one-liner to hide all markers that ar not in the current viewport.

!map.getBounds().contains(marker.getPosition()) && marker.setVisible(false);

Or,

if (map.getBounds().contains(marker.getPosition()) && !marker.getVisible()) {
    marker.setVisible(true);
} 
else if (!map.getBounds().contains(marker.getPosition()) && marker.getVisible()) {
    marker.setVisible(false);
}