[英]Filter leaflet layer by feature when you click in the map and then adding to it
I'm trying to get a map that when you click at it generates a filter of a shapefile and then adds a layer only with the feature that matches the place where you click it.我正在尝试获取一个 map,当您单击它时,它会生成一个 shapefile 过滤器,然后仅添加一个具有与您单击它的位置相匹配的功能的图层。
I find this example http://plnkr.co/edit/o5Q0p3?p=preview&preview but in my case I need that the layer isn't added to map at fist.我找到了这个例子http://plnkr.co/edit/o5Q0p3?p=preview&preview但在我的情况下,我需要该层没有添加到 map 中。 So, with only the map of leaflet you click and generates the filter of the shapefile and then you can see the feature.
因此,仅使用 leaflet 的 map 您单击并生成 shapefile 的过滤器,然后您就可以看到该功能。
This is what I have:这就是我所拥有的:
''' '''
var shpfileM = new L.Shapefile('assets/Muncipios.zip', {
onEachFeature: function (feature, layer) {
if (feature.properties) {
layer.bindPopup(Object.keys(feature.properties).map(function (k) {
return k + ": " + feature.properties[k];
}).join("<br />"), {
maxHeight: 200
});
}
},
style: {
color: 'green',
fillColor: 'green',
fillOpacity: 0.1
}
});
function onMapClick(e) {
var longlat = map.getLatLng();
eachMun = L.Shapefile(shpfileM, {
filter: filter(feature),
if (feature = longlat) { return
L.layer(feature).addTo(map)
}
})};
map.on('click', onMapClick);
Thank you a lot.十分感谢。 and I hope that many other people that might have the same question finds the answers useful: (:
我希望许多可能有相同问题的其他人发现答案很有用:(:
it is little more convenient to use shapefile-js
https://github.com/calvinmetcalf/shapefile-js instead of L.Shapefile
.使用
shapefile-js
https://github.com/calvinmetcalf/shapefile-js而不是L.Shapefile
更方便一些。 Because it allows to load geojson data once and reuse it on each click.因为它允许加载一次 geojson 数据并在每次点击时重用它。
also include turf.js还包括 turf.js
<script src="https://cdn.jsdelivr.net/npm/@turf/turf@5/turf.min.js"></script>
// create map
var mymap = L.map(...)
// create variable, where json from shp, will be once loaded and stored
let jsonData = null
// create variable for polygons group
let group = null
// read shp data and put it to variable
shp("filepath.zip").then(function (geojson) {
console.log(geojson)
jsonData = geojson
});
function onMapClick(e) {
// get point from click event and create turf point
let point = turf.point([e.latlng.lng, e.latlng.lat])
// clear polygons, if they already exist
if (group) {
group.clearLayers()
group.removeFrom(mymap)
}
group = L.geoJSON(jsonData, {
style: {...},
filter: (el) => {
// try catch, to avoid invalid geometry errors
try {
// check point in polygon
return turf.booleanPointInPolygon(point, el.geometry)
} catch(error) {
return false
}
},
onEachFeature: function(){...}
})
// add filtered layer group to map
group.addTo(mymap)
}
mymap.on('click', onMapClick);
OR using L.Shapefile或使用 L.Shapefile
function onMapClick(e) {
let point = turf.point([e.latlng.lng, e.latlng.lat])
if (group) {
group.clearLayers()
group.removeFrom(mymap)
}
group = new L.Shapefile('filepath.zip', {
// same options as in previous example
})
group.addTo(mymap)
};
mymap.on('click', onMapClick);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.