簡體   English   中英

在Google Maps V3中獲取GeoJSON數據層的屬性

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

將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')),
        });
    })
});

我正在加載的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 -array,當您要訪問其他屬性時,必須自己實現。

根據給定的代碼,它並不復雜,因為可以通過$.getJSON data將geoJson作為對象進行訪問,因此您可以簡單地通過

data.properties.maxPosts

我相信您應該能夠使用getFeatureById(id:number | string)來訪問從geoJSON添加的信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM