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"
}
}
Google Maps Platform supports GeoJSON data with a single function call.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With