本文介紹了傳單:更新 GeoJson 過濾器?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我想用數據填充 GeoJson 層,然后動態過濾要顯示的特征.
I would like to populate a GeoJson layer with data and then dynamically filter what features to show.
我已經讓過濾器功能工作,但我不知道如何更改過濾器然后刷新圖層.
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?
推薦答案
我通過將每個功能類型添加到不同的 LayerGroup 基于特征的屬性.例如
I did this by adding each feature type to a different LayerGroup based on a property of the feature. e.g.
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]
}
}
];
創建 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);
}
然后您可以調用 Leaflet map.addLayer/removeLayer 函數,例如
Then you can call the Leaflet map.addLayer/removeLayer functions e.g.
//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);
}
這篇關于傳單:更新 GeoJson 過濾器?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!