Google Maps Javascript API显式访问GeoJSON要素属性

the*_*ist 1 javascript google-maps geojson google-maps-api-3

我已经使用谷歌地图Javascript API几个星期了.在将geoJSON的属性添加到地图并成为地图的特征之后,我一直无法访问该属性.

例如,假设我有这个示例geoJSON

var geo = {"type":"FeatureCollection","features":[
              {"type":"Feature","id":"country","properties":
                  {"name":"ExampleCountry"},"geometry":  {exampleGeometry}}
          ]};
Run Code Online (Sandbox Code Playgroud)

假设我加载了geoJSON并想要访问我刚添加的功能的id属性.在这种情况下都feature.id不起作用feature.getProperty('id').通过调试,我发现我可以通过访问'id'属性feature.F.该解决方案工作正常数周,但无论出于何种原因,上周它停止工作.我必须使用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;
 });
Run Code Online (Sandbox Code Playgroud)

这似乎不是一个永久的解决方案.有谁知道这怎么可能发生?

Dr.*_*lle 5

功能的ID不是geoJSON含义中的"属性".

有一个getter方法id的a feature,使用:

 feature.getId()//should return 'country'
Run Code Online (Sandbox Code Playgroud)

当您想要获取属性(存储在properties-member中)时,请使用eg

 feature.getProperty('name')//should return 'ExampleCountry'
Run Code Online (Sandbox Code Playgroud)