Search code examples
javascriptgoogle-mapsgoogle-polyline

How to show infowindow when clicking the polyine


I'm creating a project for my internship, and I have some planes moving on a polyline in google maps as shown here.

First I tried to research for a click event when clicking the path symbol but I couldn't find anything because I need a marker. So then I tough when clicking on the polyline, but I can't find anything that works properly and I have almost no experience and that's why I am asking for help.

I have 4 of these and I need an event for each one because the infowindows will be different, I guess...

var GRU = new google.maps.Polyline({
    path: [{lat: 38.771183, lng: -9.131135}, {lat: -23.6276104, lng: -46.6568016}], //Lis - GRU
    strokeOpacity: 0.1,
    icons: [{
            icon: planeSymbol,
            offset: '0'
        }],
    map: map
});

I will appreciate a lot any help you can give to me.


Solution

  • You can add event listener with addListener:

      var handlePolyClick = function(eventArgs, polyLine) {
        // here you can handle the poly
        alert('clicked on ' + polyLine.sometitle);
      };
    
      google.maps.event.addListener(GRU, 'click', function(e) {
         handlePolyClick(e, this);
      });
    

    Working fiddle is here.

    P.S. you can find click's coordinates in e.latLng property of an event. Example with infowindow here.