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

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



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

最佳答案

考虑 geoJSON in this example

它被加载到数据层

map.data.loadGeoJson('https://storage.googleapis.com/maps-devrel/google.json');

Google over Australia 的每个字母都是一个 Feature。这些特征中的每一个都具有属性和几何形状。例如,如果您想知道每个功能的 lettercolor 属性,您可以这样做:
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

要获取给定功能的所有属性,您可以使用 Feature.forEachProperty()
map.data.forEach(function(feature) {
    console.log('>> ', feature.get('letter'), 'properties are: ');
    feature.forEachProperty(function(value,property) {
        console.log(property,':',value);
    });
});

结果是
>> 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

编辑 :正如@mitja 指出的,该方法是 getProperty ,而不是 get 。为了方便,我使用了自己设置的别名。

关于javascript - 在Google Maps javascript v3中使用feature.forEachProperty()的示例?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24144782/

10-12 12:45
查看更多