dan*_*ani 15 javascript geojson leaflet
我想用数据填充GeoJson图层,然后动态过滤要显示的功能.
我已经使过滤器功能工作,但我不知道如何更改过滤器,然后刷新图层.
添加数据后,有什么办法可以更新过滤器吗?
r8n*_*n5n 18
我通过基于功能的属性将每个要素类型添加到不同的LayerGroup来实现此目的.例如
以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]
}
}
];
Run Code Online (Sandbox Code Playgroud)
创建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);
}
Run Code Online (Sandbox Code Playgroud)
然后你可以调用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);
}
Run Code Online (Sandbox Code Playgroud)