OpenLayers 和 GeoJSON,不在同一坐标上多重标记

发布于 2024-12-09 14:39:50 字数 1040 浏览 0 评论 0原文

我的代码显示来自 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));
        });
        }
    });

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不语却知心 2024-12-16 14:39:50

为什么不使用 BBOXStrategy [1] ?

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

查看示例的js源码。

HTH,

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

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

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?

等风来 2024-12-16 14:39:50

首先,您需要使用诸如map.getLayersByName之类的方法从地图上获取图层。然后您可以迭代layer.features来查找您要添加的功能。

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

First you would need to get the layer off the map using something like map.getLayersByName. Then you can iterate over layer.features to look for the feature you are adding.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文