0

I would like to show the tooltip with the unknown property name and its value in Google Maps JavaScript API application. GeoJSON will be uploaded by the user and that's why property names are unknown. I have JS code like this:

map.data.addListener('click', function (event) {
      var value_name1 = ?
      var value_1 = event.feature.getProperty(PutHereDynamicValue);

      let html = value_name1 ":" value_1;
      infowindow.setContent();
      infowindow.setPosition(event.latLng);
      infowindow.setOptions({ pixelOffset: new google.maps.Size(0, -10) });
      infowindow.open(map);
    });
  }

How can you get dynamically from GeoJSON its property name and the value? Should I parse GeoJSON and extract properties keys?

1 Answer 1

1

Okay, figured it out by parsing GeoJSON. Here is chunk of my code in function:

 var geojson = JSON.parse(geoString);
 var propert = geojson.features[0].properties;

 keys = [];
 for (var key in propert) keys.push(key);

So generally I parse to JSON uploaded GeoJSON, extract the "properties" keys by var property, and finally add all present keys to array "keys".

Later in tooltip code I use values from keys arrays:

map.data.addListener('click', function (event) {
  let html = keys[0] + " : " + event.feature.getProperty(keys[0]);
  infowindow.setContent(html);
  infowindow.setPosition(event.latLng);
  infowindow.setOptions({ pixelOffset: new google.maps.Size(0, -10) });
  infowindow.open(map);
});
}

There is a possibility to make a loop for all keys/values here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.