Can someone provide an example of forEachProperty()?
https://developers.google.com/maps/documentation/javascript/reference#Data.Feature
forEachProperty(callback:function(*, string))
Repeatedly invokes the given function, passing a property value and name on each invocation. The order of iteration through the properties is undefined.
Either my google search is flawed or there is not a single instance of it being used in code samples on the web.
Consider the geoJSON in this example
It gets loaded to the data layer with
map.data.loadGeoJson('https://storage.googleapis.com/maps-devrel/google.json');
Each letter of the word Google over Australia is a Feature. Each of these features has properties and geometry. For example, if you wanted to know the letter
and color
properties of each feature you'd do:
map.data.forEach(function(feature) {
console.log(feature.getProperty('letter'), 'is' ,feature.getProperty('color'));
});
And the result would be
G is blue
o is red
o is yellow
g is blue
l is green
e is red
To get all the properties for a given feature, you would use Feature.forEachProperty()
map.data.forEach(function(feature) {
console.log('>> ', feature.get('letter'), 'properties are: ');
feature.forEachProperty(function(value,property) {
console.log(property,':',value);
});
});
And the result would be
>> G properties are:
letter : G
color : blue
rank : 7
ascii : 71
>> o properties are:
letter : o
color : red
rank : 15
ascii : 111
>> o properties are:
letter : o
color : yellow
rank : 15
ascii : 111
>> g properties are:
letter : g
color : blue
rank : 7
ascii : 103
>> l properties are:
letter : l
color : green
rank : 12
ascii : 108
>> e properties are:
letter : e
color : red
rank : 5
ascii : 101
Edit: As @mitja pointed out, the method is getProperty
, not get
. I was using an alias I had set myself for convenience.
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