如何引用现有GeoJSON传单对象的数据? [英] How to reference the data of an existing GeoJSON leaflet object?

查看:83
本文介绍了如何引用现有GeoJSON传单对象的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个Leaflet Map,我们正在尝试使用GeoJSON构建它.我们的数据来自实时流,因此我们首先使用空数据初始化GeoJSON:

We have a Leaflet Map which we are trying to build using GeoJSON. Our data comes in from a live stream, so we start out by initializing the GeoJSON with empty data:

var myLayer = L.geoJson([], {
  style: function(feature) {
    var color = getDataband(LATENCY_BANDS, feature.properties.latency).color;

    return { 
      fillColor: color
    };
  },

  pointToLayer: function(feature, latlng) {
    return L.circleMarker(latlng, geojsonMarkerOptions);
  },

  filter: function(feature, layer) {
    var latency = feature.properties.latency;

    for (var i = 0; i < ENABLED_LATENCY_BANDS.length; i++) {
      if (ENABLED_LATENCY_BANDS[i].contains(latency)) {
        return true;
      }
    }

    return false;
  }
});

然后,当消息通过以下方式从实时流中传入时,我们一个接一个地添加数据点:

Then we add the data points one by one as messages come in from the live stream by calling:

var geojsonFeature = {
  "type": "Feature",
  "properties": {
    "latency": sample.latency
  },
  "geometry": {
    "type": "Point",
    "coordinates": [location.longitude, location.latitude]
  }
};

myLayer.addData(geojsonFeature);

但是,我们的过滤器会根据用户输入动态更改.因此,每当用户输入更改时,我们都需要删除所有现有数据并将所有数据重新添加到地图中,以便新过滤器可以应用于所有现有数据和新数据.每当用户输入更改时,我们尝试调用以下命令:

However, our filter dynamically changes based on user input. Therefore, we need to remove all existing data and re-add all data to the map whenever the user input changes in order for the new filter to apply to all existing and new data. We tried calling the following whenever the user input changes:

    map.removeLayer(myLayer);
    myLayer.addTo(map);

但是,当myLayer重新添加到地图时,旧数据将被清除,仅显示新数据.

However, when myLayer is re-added to the map, the old data are wiped out and only new data appear.

似乎我们应该在调用myLayer.addTo(map)之前重新初始化myLayer对象,但是我们不知道如何使用现有要素数据重新初始化myLayer.文档没有解释如何引用GeoJSON传单对象的数据.

It seems like we should be re-initializing the myLayer object before we call myLayer.addTo(map), but we don't know how to re-initialize myLayer with the existing feature data. Documentation does not explain how to reference the data of the GeoJSON leaflet object.

我们如何引用GeoJSON传单对象的数据?

How do we reference the data of a GeoJSON leaflet object?

谢谢.

推荐答案

filter 选项仅在实例化和使用.addData()方法时有效.此外,过滤掉某个功能后,它不仅会被隐藏",还会被不记录在内存中(即不在该组中).

The filter option has effect only at instantiation and when using .addData() method. Furthermore, when a feature is filtered out, it is not just "hidden", it is not recorded in memory (i.e. not within that group).

很奇怪,如果您是否更改了过滤条件,则通过删除组并将其重新添加到地图中来清除旧数据.

It is strange that your old data is wiped out by removing the group and adding it back to the map, should you have changed the filter conditions or not.

无论如何,就您所要实现的意义而言,可能没有关于如何引用GeoJSON Leaflet对象的数据"的文档,因为它依赖于实现.

Anyway, there is probably no documentation about how to "reference the data of the GeoJSON Leaflet object" in the sense that you are trying to achieve, because it is implementation dependent.

在您的情况下,您需要在隐藏的组/数组中累积功能,以便每当您的过滤条件发生变化时,您都可以通过新的过滤器重新评估这些功能.

In your case, you would need to accumulate your features in a hidden group / array, so that whenever your filter conditions change, you can re-evaluate these features through the new filter.

例如,您可以执行以下操作:

For example, you could do something like:

var accumulatedFeatures = [];

// When data arrives
accumulatedFeatures.push(geojsonFeature);
myLayer.addData(geojsonFeature);

// When filter conditions change
myLayer.clearLayers(); // http://leafletjs.com/reference-1.0.0.html#layergroup-clearlayers
myLayer.addData(accumulatedFeatures); // Re-evaluate all data through new filter

这篇关于如何引用现有GeoJSON传单对象的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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