我已经使用 Google Maps Javascript API 几个星期了。在将 geoJSON 添加到 map 并成为 map 的要素后,我无法访问它的属性。
例如,假设我有这个示例 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'
当你想获得一个属性(存储在属性成员中)时使用例如
feature.getProperty('name')//should return 'ExampleCountry'
关于javascript - Google Maps Javascript API 显式访问 GeoJSON 功能属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31795793/