Ravi Ram Ravi Ram - 1 year ago 107
Javascript Question

Google Map Iterate through all the objects

I am dynamically loading json map data. Once the data is loaded, I wanted to iterate over all items (Features) on the map. Then I want to add a 'listener' when a user clicks on the feature.

map.data.forEach(function(feature) {

map.data.addListener(feature, 'click', function(event) {


When I console.log(feature) there are objects. (see image)

Other examples show map.data.addListener(polygon, 'click', function(event) {////}

Loggin shows 'feature.getGeometry().getType().toLowerCase()' = polygon so I know I have good objects.

How do I iterate through all the objects and listen for the click?
enter image description here

Answer Source

You can add a click listener to the data layer like this google example, replacing the "mouseover" with "click" (outside of the .forEach).

// Set click event for each feature.
map.data.addListener('click', function(event) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download