将geoJSON文件作为数据层加载到谷歌地图中时,如何访问数据层本身的属性?
我知道如何访问各个属性,如以下示例中的posts_here
。我希望得到的是图层本身的属性 - 在本例中,maxPosts
.
$.getJSON("http://example.com/posts/grid.json" + location.search, function (data) {
grid = map_canvas.data.addGeoJson(data);
map_canvas.data.setStyle(function(feature) {
return /** @type {google.maps.Data.StyleOptions} */({
strokeWeight: Math.log(feature.getProperty('posts_here')),
});
})
});
我正在加载的grid.json
示例:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-58,-35],
[-58,-34],
[-57,-34],
[-57,-35],
[-58,-35]
]
]
},
"properties": {
"posts_here": "177"
}
}
],
"properties": {
"maxPosts": "177"
}
}
API 仅解析 FeatureCollection 的 features
-数组,当您想要访问其他属性时,您必须自己实现它。
基于给定的代码,这并不复杂,因为 geoJson 可以通过 $.getJSON
-回调中的data
作为对象访问,您可以简单地通过以下方式访问该属性
data.properties.maxPosts
我相信
你应该能够使用getFeatureById(id:number|string)来访问你从geoJSON添加的信息。