简体   繁体   English

在Mapbox中显示嵌套GeoJson的属性

[英]Display properties of nested GeoJson in Mapbox

I am unable to query nested properties feature, which is imported from a geojson source and displayed on a mapbox map. 我无法查询嵌套属性功能,该功能是从geojson源导入的,并显示在地图框地图上。

I am able to get the feature with an on click listener, but as soon as the property is nested, the object is represented as a string 我可以通过单击监听器获得该功能,但是只要嵌套了该属性,该对象就会表示为字符串

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

Therefore I am only able to display the not-nested properties in a popup, when it is clicked. 因此,单击该对话框时,我只能在弹出窗口中显示未嵌套的属性。

my code: 我的代码:

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);

I read that you can access nested properties with expressions, but how can expressions be applied in the html of the popup? 我读到您可以使用表达式访问嵌套属性,但是如何在弹出窗口的html中应用表达式? Is there a way how I can access the nested properties or do I have to redesign the geojson's structure? 有什么方法可以访问嵌套属性,还是必须重新设计geojson的结构?

any help is highly appreciated! 非常感谢您的帮助! Thank you so much, 非常感谢,

Regards Simon 问候西蒙

Since the property values are converted into JSON strings, you need a reverse conversion: 由于属性值已转换为JSON字符串,因此需要反向转换:

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

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

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

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