我已经使用Google Maps Javascript API几个星期了。在geoJSON被添加到地图中并成为地图的一个功能后,我一直无法访问它的属性。
例如,我有这个geoJSON样本
var geo = {"type":"FeatureCollection","features":[
{"type":"Feature","id":"country","properties":
{"name":"ExampleCountry"},"geometry": {exampleGeometry}}
]};
假设我加载geoJSON并想要访问我刚刚添加的特性的id属性。在这种情况下,feature.id
和feature.getProperty('id')
都不起作用。从调试中,我发现我可以通过feature.F
访问"id"属性。这个解决方案在几周内都很有效,但由于某种原因,上周它停止了工作。我必须使用feature.K
来访问ID属性。
var mapOptions = {
center: { lat: -34.397, lng: 150.644},
zoom: 8
};
map = new google.maps.Map(document.getElementById('map-canvas'), {mapOptions});
map.data.loadGeoJson(geo);
map.data.forEach(function(feature) {
//Used to Work, randomly stopped working last week (feature.F is undefined)
var id = feature.F;
//New Solution
var id = feature.K;
});
这似乎不是一个永久的解决方案。有人知道这是怎么发生的吗?
特性的id不是geoJSON意义上的"属性"。
feature
的id
有一个getter方法,使用:
feature.getId()//should return 'country'
当你想获得一个属性(存储在properties-member中)时,使用例如
feature.getProperty('name')//should return 'ExampleCountry'