在Google Maps V3中获取GeoJSON数据图层的属性

cai*_*lin 5 javascript jquery google-maps google-maps-api-3

将geoJSON文件作为数据层加载到Google Map时,如何访问数据层本身的属性?

我知道如何访问各个属性,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')),
        });
    })
});
Run Code Online (Sandbox Code Playgroud)

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"
    }
}
Run Code Online (Sandbox Code Playgroud)

Dr.*_*lle 5

API仅解析featuresFeatureCollection的-array,当您想要访问其他属性时,您必须自己实现它.

根据给定的代码,它并不复杂,因为geoJson可以通过-callback data内部作为对象$.getJSON访问,您可以通过

data.properties.maxPosts
Run Code Online (Sandbox Code Playgroud)