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

Getting the properties of a GeoJSON data layer in Google Maps V3

本文关键字:数据 属性 GeoJSON 获取 Google Maps V3      更新时间:2023-09-26

将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添加的信息。