在Google Maps javascript v3中使用feature.forEachProperty()的示例?
有人能提供forEachProperty()的示例吗在Google Maps javascript v3中使用feature.forEachProperty()的示例?,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,有人能提供forEachProperty()的示例吗 forEachProperty(回调:函数(*,字符串)) 重复调用给定函数,传递属性值和 每个调用上的名称。通过属性的迭代顺序 没有定义 要么是我的谷歌搜索有缺陷,要么是没有一个实例被用于web上的代码示例中。考虑一下geoJSON 它被加载到数据层 map.data.loadGeoJson('https://storage.googleapis.com/maps-devrel/google.json'); 谷歌在澳大利亚的每一个字母都
forEachProperty(回调:函数(*,字符串))
重复调用给定函数,传递属性值和
每个调用上的名称。通过属性的迭代顺序
没有定义
要么是我的谷歌搜索有缺陷,要么是没有一个实例被用于web上的代码示例中。考虑一下geoJSON
它被加载到数据层
map.data.loadGeoJson('https://storage.googleapis.com/maps-devrel/google.json');
谷歌在澳大利亚的每一个字母都是一个特征。每个特征都具有特性和几何图形。例如,如果您想知道每个功能的字母
和颜色
属性,您应该:
map.data.forEach(function(feature) {
console.log(feature.getProperty('letter'), 'is' ,feature.getProperty('color'));
});
结果是
G is blue
o is red
o is yellow
g is blue
l is green
e is red
>> 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
要获取给定功能的所有属性,可以使用feature.forEachProperty()
结果是
G is blue
o is red
o is yellow
g is blue
l is green
e is red
>> 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:正如@mitja指出的,方法是
getProperty
,而不是get
。我使用了一个为方便起见自己设置的别名。//假设geoJSON对象已经加载到名为map的google map.data中
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;
}
});
});
}
应该是“功能(键,val)”而不是“功能(键,val)”