这是我的 GeoJSON 的 View :
var point_layer_WGS84_dist = {
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "x": 7.6789651, "y": 48.5066953, "distCoupe": 10000, "path": "coupes_10000_14995\\PM_10000.png" }, "geometry": { "type": "Point", "coordinates": [ 7.6789651, 48.5066953 ] } },
{ "type": "Feature", "properties": { "x": 7.6788011, "y": 48.5063054, "distCoupe": 10045, "path": "coupes_10000_14995\\PM_10045.png" }, "geometry": { "type": "Point", "coordinates": [ 7.6788011, 48.5063054 ] } },
我想从 geojson 文件中获取信息,我将其链接为 HTML 有没有可能:
var feat = point_layer_WGS84_dist.getFeatureByPropertie("distCoupe",'10000')
var imageLocation = feat.path
leaflet 可以访问它,我怎样才能访问它?
最佳答案
您可以使用Array.prototype.filter
定义getFeaturesByProperty
:
var point_layer_WGS84_dist = {
"type": "FeatureCollection",
"crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:OGC:1.3:CRS84"}},
"features": [
{
"type": "Feature",
"properties": {"x": 7.6789651, "y": 48.5066953, "distCoupe": 10000, "path": "coupes_10000_14995\\PM_10000.png"},
"geometry": {"type": "Point", "coordinates": [7.6789651, 48.5066953]}
},
{
"type": "Feature",
"properties": {"x": 7.6788011, "y": 48.5063054, "distCoupe": 10045, "path": "coupes_10000_14995\\PM_10045.png"},
"geometry": {"type": "Point", "coordinates": [7.6788011, 48.5063054]}
}]
};
point_layer_WGS84_dist.getFeaturesByProperty = function(key, value) {
return this.features.filter(function(feature){
if (feature.properties[key] === value) {
return true;
} else {
return false;
}
});
};
var feats = point_layer_WGS84_dist.getFeaturesByProperty('distCoupe', 10000);
console.log(feats);
关于javascript - 获取 GeoJSON 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43880590/