繁体   English   中英

在Mapbox中显示嵌套GeoJson的属性

[英]Display properties of nested GeoJson in Mapbox

我无法查询嵌套属性功能,该功能是从geojson源导入的,并显示在地图框地图上。

我可以通过单击监听器获得该功能,但是只要嵌套了该属性,该对象就会表示为字符串

{
 ...
  properties: {
   address: "{"place":"Bern","street":"Fischerweg","street_nr":"11","zip":"3012"}"
   price: 4500
 }
}

因此,单击该对话框时,我只能在弹出窗口中显示未嵌套的属性。

我的代码:

const features = this.queryRenderedFeatures(e.point, {
        layers: ['unclustered-point']
      });

console.log(feature.properties.price) --> 4500
console.log(feature.properties.address) --> string instead of object {"place":"Bern","street":"Fischerweg","street_nr":"11","zip":"3012"}
console.log(feature.properties.address.street) --> undefined (--> NOT WORKING because of nested property)


const popup = new mapboxgl.Popup({offset: [0, -15]})
   .setLngLat(feature.geometry.coordinates)
   .setHTML('<h3>Price: ' + feature.properties.price + '</h3>
                  <p>Street: ' + feature.properties.address.street + '</p>')--> NOT WORKING because of nested property
   .setLngLat(feature.geometry.coordinates)
   .addTo(map);

我读到您可以使用表达式访问嵌套属性,但是如何在弹出窗口的html中应用表达式? 有什么方法可以访问嵌套属性,还是必须重新设计geojson的结构?

非常感谢您的帮助! 非常感谢,

问候西蒙

由于属性值已转换为JSON字符串,因此需要反向转换:

Object.keys(feature.properties).forEach(function(key) {
  feature.properties[key] = JSON.parse(feature.properties[key]);
});

[ https://jsfiddle.net/s1d7hL82/ ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM