Google map: Add click listener to each polygon

前端 未结 2 1784
小鲜肉
小鲜肉 2020-12-14 08:51

I am working on a webapplication. I have a google map, where I add polygons from an array. I loop through that array and add the polygons to the map. I also need to add an e

2条回答
  •  别那么骄傲
    2020-12-14 09:32

    Move the code block inside the for-loop.

    //Add the click listener
        google.maps.event.addListener(p, 'click', function (event) {
            //alert the index of the polygon
            alert(p.indexID);
        });
    

    OR

    You add this to for-loop,

    p.addListener('click', clickSelection);
    

    and do this

    function clickSelection(){
    alert("Clicked");
    }
    

提交回复
热议问题