在Google Maps javascript v3中使用feature.forEachProperty()的示例?

Luc*_*s W 7 javascript google-maps google-maps-api-3

有人可以提供forEachProperty()的示例吗?

https://developers.google.com/maps/documentation/javascript/reference#Data.Feature

forEachProperty(callback:function(*, string))

重复调用给定的函数,在每次调用时传递属性值和名称.通过属性的迭代顺序是未定义的.

我的谷歌搜索存在缺陷,或者网络上的代码示例中没有使用它的单个实例.

ame*_*iel 15

请考虑此示例中的geoJSON

它被加载到数据层

map.data.loadGeoJson('https://storage.googleapis.com/maps-devrel/google.json');
Run Code Online (Sandbox Code Playgroud)

谷歌在澳大利亚这个词的每个字母都是一个特写.这些功能中的每一个都具有属性和几何.例如,如果您想知道您要执行的每个功能的属性lettercolor属性:

map.data.forEach(function(feature) { 
    console.log(feature.getProperty('letter'), 'is' ,feature.getProperty('color')); 
});
Run Code Online (Sandbox Code Playgroud)

结果就是

G is blue
o is red
o is yellow
g is blue
l is green
e is red 
Run Code Online (Sandbox Code Playgroud)

要获得给定功能的所有属性,您可以使用 Feature.forEachProperty()

map.data.forEach(function(feature) { 
    console.log('>> ', feature.get('letter'), 'properties are: '); 
    feature.forEachProperty(function(value,property) {
        console.log(property,':',value);
    });
});
Run Code Online (Sandbox Code Playgroud)

结果就是

>> 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 
Run Code Online (Sandbox Code Playgroud)

编辑:正如@mitja指出的那样,方法getProperty不是get.我使用的是为方便起见而设置的别名.