OpenLayers and GeoJSON, rather than multiplying the marker by the same coordinates

In my code, markers from GeoJSON are displayed, when I managed to zoom in 10, it loads a GeoJSON file, but how can I avoid repeating the same markers? Is there a way to check if a marker exists in 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)); }); } }); 
+6
source share
2 answers

Why not use BBOX Strategy [1]?

This will do what you need, and will certainly be more efficient (it will remove existing functions and reload new ones on zoomend ). Comparing the features to add a new one will require a lot of comparison, and you can complete too many functions on your map.

Check source js example.

Hth,

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

EDIT: if you want to change less code, calling vectorLayer.removeAllFeatures() before adding will solve your problem ... Do you really need to save functions from related ones?

+4
source

First you will need to remove the layer from the map using something like map.getLayersByName. You can then iterate over layer.features properties to find the function to add.

If you can change the backend to use BBOX, then the BBOX strategy with zoom level and projection settings will take care of you a lot.

0
source

Source: https://habr.com/ru/post/899136/


All Articles