6

我的代码显示来自 GeoJSON 的标记,当我放大到缩放级别 10 时,它会加载 GeoJSON 文件,但是如何避免输出相同的标记?有没有办法检查特定位置是否已经存在标记?编码

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));
        });
        }
    });
4

2 回答 2

4

为什么不使用BBOX Strategy[1] ?

这将满足您的需求,并且肯定会提高性能(它将删除现有功能并重新加载新功能zoomend)。比较要素以添加新要素需要大量比较,并且您的地图上可能会出现过多要素。

查看示例的 js 源代码。

高温下,

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

编辑:如果您想更改更少的代码,vectorLayer.removeAllFeatures()在添加之前调用将解决您的问题……您真的需要保持功能不受限制吗?

于 2011-10-21T12:01:50.003 回答
0

首先,您需要使用 map.getLayersByName 之类的方法将图层从地图中移除。然后您可以遍历 layer.features 以查找您要添加的功能。

如果您可以修改后端以使用 BBOX,那么具有缩放级别和投影设置的 BBOX 策略将为您提供很多帮助。

于 2011-10-22T21:39:01.780 回答