【问题标题】:Getting the properties of a GeoJSON data layer in Google Maps V3在 Google Maps V3 中获取 GeoJSON 数据层的属性
【发布时间】:2015-03-04 19:51:07
【问题描述】:

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

我知道如何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"
    }
}

【问题讨论】:

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


    【解决方案1】:

    API 只解析 FeatureCollection 的 features-array,当您想要访问其他属性时,您必须自己实现它。

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

    访问该属性
    data.properties.maxPosts
    

    【讨论】:

    • 你明白了。给自己的教训:console.log() 一切。
    【解决方案2】:

    我相信您应该能够使用 getFeatureById(id:number|string) 访问您从 geoJSON 添加的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-12
      • 2014-08-08
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多