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

标签 javascript jquery google-maps google-maps-api-3

将 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/

相关文章:

javascript - jQuery Animate 仅以一种方式工作

javascript - 使用javascript在div中通过标签选择元素

php - 使用 JQuery 将变量传递给 Controller ​​ - PHP Codeigniter

javascript - 有没有办法将 jQuery 函数作为参数传递?

javascript - jQuery 从 cookie 读取国家并放入 Google 变量

android - 谷歌地图安卓方向

javascript - 如何修改函数内部的全局变量?

javascript - 类型错误 : submitQuestion is not a function

javascript - 使用 axios 表达 promisify

google-maps - 谷歌地图 api v3 折线默认选项?