最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Example of using feature.forEachProperty() in Google Maps javascript v3? - Stack Overflow

programmeradmin0浏览0评论

Can someone provide an example of forEachProperty()?

.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.

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.

Share Improve this question asked Jun 10, 2014 at 15:20 Lucas WalterLucas Walter 9723 gold badges10 silver badges24 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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.

//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;
            }
        });         
    });
}
发布评论

评论列表(0)

  1. 暂无评论