Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>2013.08.15 Alright, updating my answer with a full-example of all js, html, css. You can fill in the ruby yourself, which will be trivial.</p> <p><a href="https://github.com/cfurrow/stackoverflow-multi-googledir" rel="nofollow">Code is also on github, here.</a></p> <p>HTML page:</p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;multi directions&lt;/title&gt; &lt;link rel="stylesheet" href="site.css" type="text/css"/&gt; &lt;/head&gt; &lt;body&gt; &lt;script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="main.js"&gt;&lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>CSS</p> <pre><code>.map{ width:500px; height:300px; border: solid 1px #000; margin-bottom:10px; } .directions{ width:500px; border: dashed 1px #000; margin-bottom: 10px; } </code></pre> <p>JS</p> <pre><code>function DirectionHelper(id) { var rendererOptions, chicago, mapOptions; this.directionsDisplay = null; this.id = id; this.map = null; this.directionsService = new google.maps.DirectionsService(); var startElement = document.createElement('input'); startElement.id = 'start_'+id; startElement.value = 'The Loop'; document.body.appendChild(startElement); var endElement = document.createElement('input'); endElement.id = 'end_'+id; endElement.value = 'Grant Park'; document.body.appendChild(endElement); var mapElement = document.createElement('div'); mapElement.id = "map_" + id; mapElement.className = 'map'; document.body.appendChild(mapElement); var directionsPanelElement = document.createElement('div'); directionsPanelElement.id = 'directions_panel'+id; directionsPanelElement.className='directions'; document.body.appendChild(directionsPanelElement); rendererOptions = { draggable: true, panel: document.getElementById('directions_panel' + this.id) }; this.directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions); chicago = new google.maps.LatLng(41.850033, -87.6500523); mapOptions = { zoom: 6, center: chicago, mapTypeId: google.maps.MapTypeId.ROADMAP } this.map = new google.maps.Map(document.getElementById("map_"+this.id), mapOptions); this.directionsDisplay.setMap(this.map); } DirectionHelper.prototype.calcRoute = function(start, end, waypoints){ var request, self = this; request = { origin: start, // an address or LatLng destination: end, // an address or a LatLng travelMode: google.maps.TravelMode.DRIVING }; console.log(request) this.directionsService.route(request,function(r,s){ console.log('shit') }) this.directionsService.route(request, function(response, status) { var theRoute, summaryPanel; if (status == google.maps.DirectionsStatus.OK) { self.directionsDisplay.setDirections(response); theRoute = response.routes[0]; summaryPanel = document.getElementById("directions_panel" + self.id); summaryPanel.innerHTML = ""; // For each route, display summary information. for (var i = 0; i &lt; theRoute.legs.length; i++) { var routeSegment = i+1; summaryPanel.innerHTML += "&lt;b&gt;Route Segment: " + routeSegment + "&lt;/b&gt;&lt;br /&gt;"; summaryPanel.innerHTML += theRoute.legs[i].start_address + " to "; summaryPanel.innerHTML += theRoute.legs[i].end_address + "&lt;br /&gt;"; summaryPanel.innerHTML += theRoute.legs[i].distance.text + "&lt;br /&gt;"; summaryPanel.innerHTML += theRoute.legs[i].duration.text + "&lt;br /&gt;&lt;br /&gt;"; } } if(status == google.maps.DirectionsStatus.ZERO_RESULTS){ alert("Error: One or more of your addresses was not found. If you think this is an error with our website, please contact us."); } if(status == google.maps.DirectionsStatus.MAX_WAYPOINTS_EXCEEDED){ alert("Error: You have included more than eight stops along the way. Please use only eight or fewer stops, and try again."); } if(status == google.maps.DirectionsStatus.INVALID_REQUEST){ alert("Error: You may be missing a starting or ending point, or you may have included two starting points or two ending points: one in the dropdown menu and one in the entry box. Please edit your map and try again."); } if(status == google.maps.DirectionsStatus.NOT_FOUND){ alert("Error: One or more of your addresses was not found. If you think this is an error with our website, please contact us."); } if(status == google.maps.DirectionsStatus.OVER_QUERY_LIMIT){ alert("We're sorry! This is an internal error with Go See Campus. Please contact us so we can resolve it."); } if(status == google.maps.DirectionsStatus.UNKNOWN_ERROR){ alert("Error: Something went wrong when you loaded this page. Try loading the page again. You may need to log out, clear your temporary files, and log back in."); } }); }; function initialize() { var dirHelper = null; for(var i=1; i&lt;6; i++){ dirHelper = new DirectionHelper(i); dirHelper.calcRoute(document.getElementById('start_'+i).value, document.getElementById('end_'+i).value); } } google.maps.event.addDomListener(window, 'load', initialize); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload