Google Maps Javascript API access GeoJSON feature property explicitly - Stack Overflow

admin2025-04-19  0

I've been working with the Google Maps Javascript API for several weeks now. I've been having trouble accessing a property of a geoJSON after it has been added to the map and bees a feature of the map.

For example, lets say I have this sample geoJSON

var geo = {"type":"FeatureCollection","features":[
              {"type":"Feature","id":"country","properties":
                  {"name":"ExampleCountry"},"geometry":  {exampleGeometry}}
          ]};

Lets say I load the geoJSON and want to access the id property of the feature I have just added. Neither feature.id nor feature.getProperty('id') works in this case. From debugging, I found out that I can access the 'id' property via feature.F. That solution was working fine for several weeks, but for whatever reason, last week it ceased to work. I instead have to use feature.K to access the ID property.

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;
 });

This doesn't seem to be a permanent solution. Does anyone have any idea how this could have happened?

I've been working with the Google Maps Javascript API for several weeks now. I've been having trouble accessing a property of a geoJSON after it has been added to the map and bees a feature of the map.

For example, lets say I have this sample geoJSON

var geo = {"type":"FeatureCollection","features":[
              {"type":"Feature","id":"country","properties":
                  {"name":"ExampleCountry"},"geometry":  {exampleGeometry}}
          ]};

Lets say I load the geoJSON and want to access the id property of the feature I have just added. Neither feature.id nor feature.getProperty('id') works in this case. From debugging, I found out that I can access the 'id' property via feature.F. That solution was working fine for several weeks, but for whatever reason, last week it ceased to work. I instead have to use feature.K to access the ID property.

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;
 });

This doesn't seem to be a permanent solution. Does anyone have any idea how this could have happened?

Share Improve this question edited Aug 3, 2015 at 20:37 theBoarialist asked Aug 3, 2015 at 20:02 theBoarialisttheBoarialist 331 silver badge5 bronze badges 4
  • Do not use undocumented properties of the API, they can and do change with every release. Use the documented functions. – geocodezip Commented Aug 3, 2015 at 20:16
  • Please provide a Minimal, Complete, Tested and Readable example that demonstrates the issue. – geocodezip Commented Aug 3, 2015 at 20:23
  • What would you propose instead? – theBoarialist Commented Aug 3, 2015 at 20:42
  • Reading the documentation: feature.getId() – geocodezip Commented Aug 3, 2015 at 21:10
Add a ment  | 

1 Answer 1

Reset to default 7

The id of a feature is not a "property" in the meaning of geoJSON.

There is a getter-method for the id of a feature, use:

 feature.getId()//should return 'country'

When you want to get a property(stored in the properties-member) use e.g.

 feature.getProperty('name')//should return 'ExampleCountry'
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745063245a282828.html

最新回复(0)