最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting the properties of a GeoJSON data layer in Google Maps V3 - Stack Overflow

programmeradmin3浏览0评论

When loading a geoJSON file into a Google Map as a data layer, how does one access the properties of the data layer itself?

I know how to access the individual properties, like posts_here in the below example. What I'm looking to get is the properties for the layer itself- in this example, maxPosts.

$.getJSON(".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')),
        });
    })
});

Example of the grid.json I'm loading:

{
    "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"
    }
}

When loading a geoJSON file into a Google Map as a data layer, how does one access the properties of the data layer itself?

I know how to access the individual properties, like posts_here in the below example. What I'm looking to get is the properties for the layer itself- in this example, maxPosts.

$.getJSON("http://example./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')),
        });
    })
});

Example of the grid.json I'm loading:

{
    "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"
    }
}
Share Improve this question edited Jan 7, 2015 at 2:47 caitlin asked Jan 7, 2015 at 2:38 caitlincaitlin 2,8294 gold badges31 silver badges65 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

The API does only parse the features-array of a FeatureCollection, when you want to access additional properties you must implement it on your own.

Based on the given code it isn't plicated, because the geoJson is accessible as object via data inside the $.getJSON-callback, you may simply access the property via

data.properties.maxPosts

I believe you should be able to use getFeatureById(id:number|string) to access information you've added from your geoJSON.

发布评论

评论列表(0)

  1. 暂无评论