I would like to display multiple Google Map routes on a single Google map. Data is taken from an XML file with multiple markers. Each marker has a start point and an end point (in the form of latitude and longitude values). These tokens are then added to the map, and a call to the Google Maps Directions Service is made to route between the start and end points of each token.
The problem I am facing is that only one route is drawn (if I refresh the page, it seems to just select one of the markers at random and draw directions between the two markers). Other markers are not displayed at all.
I have a console.logged for loop to verify that it is running for each token in XML (it is). I assume this is related to these two lines, but this assumption ...
directionsDisplay = new google.maps.DirectionsRenderer(); directionsService = new google.maps.DirectionsService();
I read this question - Using the Google Maps 3 API to get multiple routes on a map , but I donβt really understand the answer / know how appropriate this is my situation. Thanks.
jQuery(document).ready(function($) { var map = new google.maps.Map(document.getElementById("map_canvas"), { zoom: 13, mapTypeId: 'roadmap' }); // Try HTML5 geolocation if(navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); map.setCenter(pos); map.setZoom(13); }, function() { handleNoGeolocation(true); }); } else { // Browser doesn't support Geolocation handleNoGeolocation(false); } directionsDisplay = new google.maps.DirectionsRenderer(); directionsService = new google.maps.DirectionsService(); // Change this depending on the name of your PHP file downloadUrl("xml.php", function(data) { var xml = data.responseXML; var markers = xml.documentElement.getElementsByTagName("marker"); for (var i = 0; i < markers.length; i++) { var title = markers[i].getAttribute("title"); mode = markers[i].getAttribute("mode"); startpoint = new google.maps.LatLng( parseFloat(markers[i].getAttribute("startlat")), parseFloat(markers[i].getAttribute("startlng"))); endpoint = new google.maps.LatLng( parseFloat(markers[i].getAttribute("endlat")), parseFloat(markers[i].getAttribute("endlng"))); calcRoute(); console.log('marker'); } }); directionsDisplay.setMap(map); }); function calcRoute() { var request = { origin: startpoint, destination: endpoint, //waypoints: waypts, travelMode: google.maps.DirectionsTravelMode[mode] }; directionsService.route(request, function(response, status) { if (status == google.maps.DirectionsStatus.OK) { directionsDisplay.setDirections(response); } }); } function downloadUrl(url, callback) { var request = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest; request.onreadystatechange = function() { if (request.readyState == 4) { request.onreadystatechange = doNothing; callback(request, request.status); } }; request.open('GET', url, true); request.send(null); } function doNothing() {}
source share