javascript 传单:更新 GeoJson 过滤器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16148598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Leaflet: Update GeoJson filter?
提问by dani
I would like to populate a GeoJson layer with data and then dynamically filter what features to show.
我想用数据填充 GeoJson 层,然后动态过滤要显示的特征。
I have gotten the filter function to work but I do not know how to change the filter and then refresh the layer.
我已经使过滤器功能正常工作,但我不知道如何更改过滤器然后刷新图层。
Is there any way I can update the filter after I have added the data?
添加数据后,有什么方法可以更新过滤器?
回答by r8n5n
I did this by adding each feature type to a different LayerGroupbased on a property of the feature. e.g.
我通过根据要素的属性将每个要素类型添加到不同的LayerGroup来做到这一点。例如
GeoJSON
GeoJSON
var data =[
{
type: "Feature",
properties: {
type: "type1"
},
geometry: {
type: "Point",
coordinates: [-1.252,52.107]
}
},
{
type: "Feature",
properties: {
type: "type2"
},
geometry: {
type: "Point",
coordinates: [-2.252,54.107]
}
}
];
Create the GeoJSONLayer
创建GeoJSON层
//array to store layers for each feature type
var mapLayerGroups = [];
//draw GEOJSON - don't add the GEOJSON layer to the map here
L.geoJson(data, {onEachFeature: onEachFeature})//.addTo(map);
/*
*for all features create a layerGroup for each feature type and add the feature to the layerGroup
*/
function onEachFeature(feature, featureLayer) {
//does layerGroup already exist? if not create it and add to map
var lg = mapLayerGroups[feature.properties.type];
if (lg === undefined) {
lg = new L.layerGroup();
//add the layer to the map
lg.addTo(map);
//store layer
mapLayerGroups[feature.properties.type] = lg;
}
//add the feature to the layer
lg.addLayer(featureLayer);
}
Then you can call the Leaflet map.addLayer/removeLayerfunctions e.g.
然后你可以调用 Leaflet map.addLayer/removeLayer函数,例如
//Show layerGroup with feature of "type1"
showLayer("type1");
/*
* show/hide layerGroup
*/
function showLayer(id) {
var lg = mapLayerGroups[id];
map.addLayer(lg);
}
function hideLayer(id) {
var lg = mapLayerGroups[id];
map.removeLayer(lg);
}
回答by flup
In the GeoJSONaddData
method, the first check is if the data is a collection of features, in which case the method gets called for each feature.
在GeoJSONaddData
方法中,首先检查数据是否是要素的集合,在这种情况下,每个要素都会调用该方法。
Then the filter is applied as follows:
然后过滤器应用如下:
var options = this.options;
if (options.filter && !options.filter(geojson)) { return; }
So if the filter filters out the data when you add it, it does not get stored or remembered anywhere. Changing the filter won't make the data suddenly reappear.
因此,如果过滤器在您添加数据时过滤掉了数据,则它不会在任何地方存储或记住。更改过滤器不会使数据突然重新出现。
You can keep a reference to the geojson and re-initialize the layer when you change the filter.
您可以保留对 geojson 的引用,并在更改过滤器时重新初始化图层。
回答by Phuong Huynh
See example below, you have a map and a myBus layer.
请参见下面的示例,您有一个地图和一个 myBus 图层。
map.removeLayer(myBus);
...add your data edit something ...
myBus.addTo(map);