Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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"
    }
}
like image 295
caitlin Avatar asked Jan 07 '15 02:01

caitlin


People also ask

Does Google Maps use GeoJSON?

Google Maps Platform supports GeoJSON data with a single function call.

How do I import GeoJSON into Google Maps?

Loading data from the same domain The Google Maps Data Layer provides a container for arbitrary geospatial data (including GeoJSON). If your data is in a file hosted on the same domain as your Maps JavaScript API application, you can load it using the map. data. loadGeoJson() method.

How do I visualize GeoJSON data?

Open “QGIS Browser” on your computer and browse the . geojson file in the left pane. The right pane will show you a quick preview of the data points.


1 Answers

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 complicated, because the geoJson is accessible as object via data inside the $.getJSON-callback, you may simply access the property via

data.properties.maxPosts
like image 77
Dr.Molle Avatar answered Oct 06 '22 00:10

Dr.Molle