1

I want to set click listeners on many different shapes on my map. Each shape has a bunch of data which I want to show when I click on it.

var newData = new google.maps.Data({
    map: map,
    style: map.data.getStyle()
});
for (var i = 0; i < shapes.length; i++) {
    var userObject = JSON.parse(shapes[i].area);
    newData.addGeoJson(userObject);

    // It would be simplest to add a clicklistener here since i got access to the shapes[i] with the data to act on
    google.maps.event.addListener( /* ??? */, 'click', function (event) {
            alert("y");
        });
}
map.data.setMap(null);
map.data = newData;

I tried setting the click listener on the returned array from addGeoJson(...) but maybe im approaching this wrong. Maybe I need to get polygons somehow and then go about it like in this official example or Im thinking I could do something hacky with different layers.

How would you do this, given that you cannot change that you get a bunch of separate geoJson chunks? Can I get my geoJson as polygons when I'm adding them?

1 Answer 1

1

The way forward I will use which I think will work out well is:

addGeoJson returns an array of features. Use setProperty(name, newVal) on them and add the information later required by the clicking action. Retrieve the data by getProperty(name)

Add a click listener on the entire data object:

google.maps.event.addListener(newData, 'click', function (event) {
    event.feature.forEachProperty(function (value, name) {
        alert("name: " + name + "value: " + value);
    })
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.