将 geoJSON 文件作为数据层加载到 Google map 时,如何访问数据层本身的属性?
我知道如何 access the individual properties ,例如下面示例中的 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
作为对象访问,您可以简单地访问属性(property)来自
data.properties.maxPosts
关于javascript - 在 Google Maps V3 中获取 GeoJSON 数据层的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27810639/