admin管理员组

文章数量:1316376

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.

本文标签: javascriptGetting the properties of a GeoJSON data layer in Google Maps V3Stack Overflow