I'm trying to replicate similar functionality in Google Maps where you right click & it gives an option to have directions to/from that point. The struggle I'm having is interacting with the infowindow & passing the lat/longs back.
The listener creates the infowindow fine. but I can;t work out how to pass the lat/longs back into the javascript (either as an origin or destination) so as I can then use it in the directions service. I'll eventually have a second marker which will populate the other value in origin/destination. Once the array has two values I'll call the directions service.
All the examples I've seen have required some form of manual input to define the second part of the address.
I'm still very new to this, so please go easy on me; I've tried my best to provide the most trimmed down sample code to demonstrate my issue.
var mapCanvas = "map-canvas",
map,
infowindow = new google.maps.InfoWindow(),
LocationArr = [],
o;
google.maps.event.addDomListener(window, "load", function(){
map = new google.maps.Map(document.getElementById(mapCanvas), {zoom: 13,center: {lat: 53.4723272,lng: -2.2935022}});
var geocoder = new google.maps.Geocoder;
google.maps.event.addListener(map, "click", function(o) {
LocationArr.push(o.latLng);
var a = new google.maps.Marker({
position: o.latLng,
map: map,
});
var content = "<input type='button' name='DirFrom' value='Directions from here' onclick='DirFromHere()' id='Dir_From'>"
+ "<input type='button' name='DirTo' value='Directions to here' onclick='DirToHere()' id='Dir_To'>"
infowindow.setContent(content);
infowindow.open(map, a);
});
});
function DirFromHere(LocationArr){
console.log(LocationArr.length);
}
function DirToHere(LocationArr){
LocationArr=[];
}
html, body {
height: 100%;
width: 100%;
}
#map-canvas {
position: absolute;
top: 0%;
left: 0%;
width: 100%;
height: 100%;
}
<html>
<head>
<link href="css/styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="map-canvas"></div>
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<script src="js/aqrouting.js"></script>
</body>
</html>