Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenLayers, and GeoJSON, not multiply marker on same coordinates

My code is showing markers from GeoJSON, when I'm haved zoomed into zoom-level 10,it load the GeoJSON-file, but how do I avoid to reput out the same markers? Is there a way to check if there already exist a marker on a specific place? The code

map.events.register("zoomend", null, function(){

      if(map.zoom == 10)
      {
        var bounds = map.getExtent();
        console.log(bounds);
        var ne = new OpenLayers.LonLat(bounds.right,bounds.top).transform(map.getProjectionObject(),wgs84);
        var sw = new OpenLayers.LonLat(bounds.left,bounds.bottom).transform(map.getProjectionObject(),wgs84);
        var vectorLayer = new OpenLayers.Layer.Vector();
        map.addLayer(vectorLayer);
        $.getJSON('ajax.php?a=markers&type=json&sw=('+sw.lon+','+sw.lat+')&ne=('+ne.lon+','+ne.lat+')',function(data){
        //$.getJSON('test.json',function(data){
            var geojson_format = new OpenLayers.Format.GeoJSON({
                'externalProjection': wgs84,
                'internalProjection': baseProjection
                });
            vectorLayer.addFeatures(geojson_format.read(data));
        });
        }
    });
like image 906
Max Allan Avatar asked Oct 12 '11 15:10

Max Allan


1 Answers

Why not use the BBOX Strategy [1] ?

That will do what you need, and will for sure be more performant (it will delete existing features and reload new ones on zoomend). Comparing features to add new will need a lot of comparison, and you can end with too much features on your map.

Check out the js source of the example.

HTH,

1 - http://openlayers.org/dev/examples/strategy-bbox.html

EDIT: if you want to change less code, a call to vectorLayer.removeAllFeatures() before adding will solve your problem… Do you really need to keep features out of bound?

like image 137
tonio Avatar answered Nov 16 '22 07:11

tonio