javascript - 获取 GeoJSON 属性

标签 javascript html geojson

这是我的 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/

相关文章:

javascript - 使用父函数jquery显示类名

javascript - jquery ajax 不发送请求

javascript - openlayers3 尝试与 GeoJSON 交互添加功能,但无论我放什么,我总是在非洲东南部的一个地方得到一分

d3.js - d3 svg 元素位于单独 HTML 元素的背景颜色(但不是内容)之上

javascript - 4 个切换按钮相互之间使用 javascript,但它们都不是好的倾听者

javascript - 如何在jspdf中将黑色文本放在灰色矩形的顶部

php - Autocompleteplus如何检测用户语言(真实语言而不是浏览器)?

html - HTML INPUT radio 元素文本的 CSS 对齐方式

python - lxml(或 lxml.html): print tree structure

leaflet - 在 geoJSON 功能上使用 getBounds