javascript - Google Maps Javascript API 显式访问 GeoJSON 功能属性

标签 javascript google-maps google-maps-api-3 geojson

我已经使用 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.idfeature.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 意义上的“属性”。

featureid 有一个 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/

相关文章:

javascript - IE 中的热点不正确

ruby-on-rails - 谷歌地图和 Rails 3

javascript - 我们可以在 JavaScript 中使用 doNotRemoveChild() 吗?

google-maps - 新的 Places API Billing 如何处理 Session?

ios - 如何在iOS环境中访问Google Map

android - Google Geocoding API 限制

javascript - 如何从一组在 google map api 中映射的地理位置点获取行进距离?

php - 服务器端集群?

javascript - firebase -> 日期顺序反转

javascript - 在chartjs中渲染Rails json数据