javascriptjquerygoogle-mapsgoogle-maps-api-3jquery-gmap3

How to get onclick event of marker generated by getroute


In this jsfiddle is simplified version of my js: http://jsfiddle.net/Drecker/2m4kvxb8/4/ Note that interesting part of the jsfiddle is only showRoute method. And method showMarker only shows desired behavior on normal marker.

Basically I generate a route via gmap3 getroute with some waypoints. After clicking on a waypoint I need to open a small infobox with more custom information of that point - so basically somehow get onclick event of such waypoint (with some identification of that waypoint so I would be able to get proper information). I'm able to achieve desired behavior on a separate marker (as you can see in the jsfiddle - that's the fully functional separate marker on the top left), but not on the markers generated by directionrenderer.

Furthermore please note that my waypoints have stopover: false and such markers for some reason ignore (some) options like title, as you can see in jsfiddle.

Any help is very appreciated - I've tried several things none of them works.


Solution

  • There is no such option, according to the documentation and a lot of similar questions here on the stackoverflow, you can't bind click action to waypoints.

    I have a workaround for that problem. The main idea is to add markers instead of waypoints and change their icon. Marker has much more options than waypoint. So I removed waypoints and added markers. Note that you have to be much more precise when adding marker's location

    options without waypoints:

    options: {origin: {lat:49.9, lng: 14.9},
              destination: {lat: 50.1, lng: 15.1},
              travelMode: google.maps.DirectionsTravelMode.DRIVING
             },
    

    added markers with a new icon and click event:

    marker:{
        values:[
          {latLng:[49.96485, 14.88392], data:"Waypoint1", options:{icon: "http://mt.google.com/vt/icon/name=icons/spotlight/directions_transfer_icon_10px.png&scale=1"}},
          {latLng:[49.97730, 14.88185], data:"Waypoint2", options:{icon: "http://mt.google.com/vt/icon/name=icons/spotlight/directions_transfer_icon_10px.png&scale=1"}}
        ],
        options:{
          draggable: false
        },
        events:{
          click: function(marker, event, context){
            var map = $(this).gmap3("get"),
              infowindow = $(this).gmap3({get:{name:"infowindow"}});
            if (infowindow){
              infowindow.open(map, marker);
              infowindow.setContent(context.data);
            } else {
              $(this).gmap3({
                infowindow:{
                  anchor:marker, 
                  options:{content: context.data}
                }
              });
            }
          }
        }
      }
    

    Here is my workaround for that problem : DEMO