9

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.

2 Answers 2

17

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.

1

//assumes that a geoJSON object is already loaded to a google map.data called map

this.LoadFeatureProperties=function(){ //iterate through all features of the loaded geoJSON
    map.data.forEach(function(feature){
        feature.forEachProperty(function(val,key){ //iterate through all properties of each feature
            //alert(key+"____"+val);  //-----------------debug only-------------
            //note function returns value and key in reverse order from key:value
            if(/^clickable|visible|zIndex|cursor|icon|shape|title|strokeColor|strokeOpacity|strokeWeight|fillColor|fillOpacity$/.test(key)){
                map.data.overrideStyle(feature, {[key]:val}); //key must be dereferenced ;( --> alternative is: var o={};o[key]=val;
            }
        });         
    });
}
1
  • Should be 'function(key, val)' not 'function(val, key)' Commented Feb 5, 2020 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.