在 Mapbox 中显示嵌套 GeoJson 的属性
Display properties of nested GeoJson in Mapbox
我无法查询嵌套属性功能,它是从 geojson 源导入并显示在 mapbox 地图上的。
我可以通过单击侦听器获得该功能,但是一旦 属性 被嵌套,该对象就表示为字符串
{
...
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]);
});
我无法查询嵌套属性功能,它是从 geojson 源导入并显示在 mapbox 地图上的。
我可以通过单击侦听器获得该功能,但是一旦 属性 被嵌套,该对象就表示为字符串
{
...
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]);
});