就地更新传单GeoJSON功能 [英] in-place Update Leaflet GeoJSON feature

查看:61
本文介绍了就地更新传单GeoJSON功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望GeoJSON.addData()将返回GeoJSON对象的新创建的subLayer,但不会.为什么我需要这样的东西?

I was hoping that GeoJSON.addData() would return the newly created subLayer of the GeoJSON object, but it does not. Why Do I need something like this?

(当前使用Leaflet 1.0Beta2)

(currently using Leaflet 1.0Beta2)

我正在使用Leaflet GeoJson在GeoJSON(点,线,面)中显示实时数据.这是一个CRUD界面(创建,更新和删除).我收到带有GeoJSON数据的WebSocket消息,每个消息都有一个GUID.

I am using Leaflet GeoJson to show live data in a GeoJSON (point, line, polygon). It is a CRUD interface (Create, Update and Delete). I receive WebSocket messages with GeoJSON data, each one with a GUID.

对于创建的情况,我只是在相应的图层上添加了GeoJSon.AddData().

I the case of a CREATE I just do a GeoJSon.AddData() to the appropriate layer.

但是对于 UPDATE Delete ,我想要添加到GeoJSON的图层的句柄,以便我可以更新其位置或更新Geometry. addData没有给我这个句柄.从onEachFeature()或pointToLayer()获取它确实很困难

But for the UPDATE and DELETE I want a handle for the layer that was added to the GeoJSON so that I can update its location, or update the Geometry. addData is not giving me this handle. And it is really hard to get it from onEachFeature() or from pointToLayer()

当前,我确实有一种可行的方法,但是很难看.我要做的是使用GeoJSon.eachLayer(fn)搜索整个图层 每当发生更新或删除时.好像有点贵.

Currently, I do have a way that works but ugly. I have to do is search the entire layer with GeoJSon.eachLayer(fn) whenever an update or delete occurs. It seems a bit expensive.

{即使Leaflet并不是为实时的实时数据显示而真正设计的,它仍在工作,如果您不能用它来观看大量的传感器数据(IoT),这似乎很可悲.)

{even if Leaflet is not truly engineered for this live r/t display of data, it is working, and it seems sad if you cannot use it for watching a lot of sensor data, IoT) as we are doing.

this.delete = function (layerName, feature) {
    if (!(layerName in self.myLayers)) {
        alert("live.display: Missing Layer: " + layerName);
        return;
    }
    var layerInfo = Live.myLayers[layerName];
    var base = layerInfo.layer;
    var id = feature.properties.objectID;
    this.find(layerName, id, function (layer) {
        this.removeLayer(layer);
        Tools.debug(16, "live.delete:", "killed object: " + id);
    });
}
this.find = function (layerName, id, action) {
    var base = Live.myLayers[layerName].layer;
    base.eachLayer(function (feature) {
        if (!("objectID" in feature.feature.properties)) { return; }
        var objectID = feature.feature.properties.objectID;
        if (objectID == id) {
            action.call(base, feature);
        }
    });
}

推荐答案

代替(或并行地)将所有已创建的GeoJSON功能合并"到单个Leaflet GeoJSON图层组(您可以使用addData进行操作),为什么不这样做呢?首先在其自己的Leaflet GeoJSON层中创建每个功能,以便为您提供所需的手柄(然后您可以将该手柄简单地记录在对象/映射中,例如,键为您的objectID)?

Instead (or in parallel) of "merging" all created GeoJSON features into a single Leaflet GeoJSON layer group (which you do with addData), why not creating first each feature in its own Leaflet GeoJSON layer, so that it gives you the handle you are looking for (then you could simply record this handle in an object / mapping with the key being your objectID for example)?

如果需要,您甚至可以在此之后将各个图层合并到单个GeoJSON图层组中.

If desired, you could even still merge the individual layers into your single GeoJSON layer group after that.

var myGeoJsonLayerGroup = L.geoJson().addTo(map);
var myFeaturesMap = {};

function addNewFeatureToGeoJsonLayerGroup(newGeoJsonData) {
  var newGeoJSONfeature = L.geoJson(newGeoJsonData);
  myFeaturesMap[newGeoJsonData.properties.objectID] = newGeoJSONfeature;
  myGeoJsonLayerGroup.addLayer(newGeoJSONfeature);
}

function updateFeature(updatedGeoJsonData) {
  var updatedFeature = myFeaturesMap[updatedGeoJsonData.properties.objectID];
  updatedFeature.clearLayers(); // Remove the previously created layer.
  updatedFeature.addData(updatedGeoJsonData); // Replace it by the new data.
}

function deleteFeature(deletedGeoJsonData) {
  var deletedFeature = myFeaturesMap[deletedGeoJsonData.properties.objectID];
  myGeoJsonLayerGroup.removeLayer(deletedFeature);
}

演示(不使用GeoJSON): http://jsfiddle.net/ve2huzxw/94/

Demo (not using GeoJSON): http://jsfiddle.net/ve2huzxw/94/

一个稍微简单的解决方案是通过GeoJSON图层组的onEachFeature函数存储对每个单独图层的引用:

A slightly more simple solution would be to store the reference to each individual layer through the onEachFeature function of the GeoJSON layer group:

var myFeaturesMap = {};

var myGeoJsonLayerGroup = L.geoJson({
    onEachFeature: function (feature, layer) {
        myFeaturesMap[feature.properties.objectID] = layer;
    }
}).addTo(map);

function addNewFeatureToGeoJsonLayerGroup(newGeoJsonData) {
    myGeoJsonLayerGroup.addData(newGeoJsonData);
}

function updateFeature(updatedGeoJsonData) {
    deleteFeature(updatedGeoJsonData); // Remove the previously created layer.
    addNewFeatureToGeoJsonLayerGroup(updatedGeoJsonData); // Replace it by the new data.
}

function deleteFeature(deletedGeoJsonData) {
    var deletedFeature = myFeaturesMap[deletedGeoJsonData.properties.objectID];
    myGeoJsonLayerGroup.removeLayer(deletedFeature);
}

这篇关于就地更新传单GeoJSON功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆