Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
- <meta charset="utf-8">
- <title>Waypoints in directions</title>
- <style>
- html, body, #map-canvas {
- height: 100%;
- margin: 0px;
- padding: 0px
- }
- #panel {
- position: absolute;
- top: 5px;
- left: 50%;
- margin-left: -180px;
- z-index: 5;
- background-color: #fff;
- padding: 5px;
- border: 1px solid #999;
- }
- </style>
- <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
- <script>
- var directionsDisplay;
- var directionsService = new google.maps.DirectionsService();
- var map;
- var batches = [];
- var waypts = [];
- function initialize() {
- directionsDisplay = new google.maps.DirectionsRenderer();
- var chicago = new google.maps.LatLng(50.797378, 0.058504);
- var mapOptions = {
- zoom: 6,
- center: chicago
- }
- map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
- directionsDisplay.setMap(map);
- waypts.push({location:new google.maps.LatLng(50.797378, 0.058504),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.84066183,-0.4950805),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.84042033,-0.505672),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.84042033,-0.505672),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.84042033,-0.505672),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.84042033,-0.505672),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82321517,-0.5501035),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.818442,-0.55710767),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.818442,-0.55710767),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.818442,-0.55710767),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.818442,-0.55710767),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.818442,-0.55710767),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.818442,-0.55710767),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.818442,-0.55710767),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.818442,-0.55710767),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.818442,-0.55710767),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.818442,-0.55710767),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.818442,-0.55710767),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.8561195,-0.65900267),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.8561195,-0.65900267),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.8561195,-0.65900267),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.8561195,-0.65900267),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.85703483,-0.65348833),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.86659867,-0.630018),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.85330383,-0.63498383),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.8549755,-0.64712067),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.85585783,-0.64961117),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.85585783,-0.64961117),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.85886017,-0.5698375),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.85099883,-0.5634535),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.85099883,-0.5634535),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.84219867,-0.57915867),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.829614,-0.57823483),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.829614,-0.57823483),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.829614,-0.57823483),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.829614,-0.57823483),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.829614,-0.57823483),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.829614,-0.57823483),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.829614,-0.57823483),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.829614,-0.57823483),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.829614,-0.57823483),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.829614,-0.57823483),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.83001617,-0.57811617),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82539533,-0.589086),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82540817,-0.58928967),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82540817,-0.58928967),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82630817,-0.58845017),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.825811,-0.587971),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.8258645,-0.587609),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.8257455,-0.58849317),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.8257455,-0.58849317),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.825419,-0.588395),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81279,-0.58203433),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81258683,-0.58425567),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81258683,-0.58425567),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81258683,-0.58425567),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81258683,-0.58425567),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81258683,-0.58425567),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81258683,-0.58425567),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81258683,-0.58425567),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81258683,-0.58425567),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81258683,-0.58425567),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.812878,-0.58418683),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.812878,-0.58418683),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81260833,-0.5845555),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81270767,-0.58492633),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81306267,-0.58496883),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.8140175,-0.58380567),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81464667,-0.58437),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81415467,-0.58320367),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81415467,-0.58320367),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81415467,-0.58320367),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.8159215,-0.59695883),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81674783,-0.59651817),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81956917,-0.5979725),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81263917,-0.555177),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81218967,-0.55584333),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81218967,-0.55584333),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81218967,-0.55584333),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.811967,-0.55665983),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81289617,-0.55730517),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81317783,-0.55531783),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81269983,-0.55543767),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81013483,-0.55427633),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.8058155,-0.54582017),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82045,-0.60802217),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81647467,-0.60886433),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81474883,-0.61240333),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.84229283,-0.61525017),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.84536767,-0.62228383),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.846593,-0.6220225),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.84460583,-0.61934483),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.8471485,-0.61402167),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.8471485,-0.61402167),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.8471485,-0.61402167),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.8501855,-0.616509),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.85377917,-0.56846533),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.85073717,-0.57604567),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.85073717,-0.57604567),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.85476517,-0.55430183),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.86316917,-0.55118767),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.85339033,-0.55129333),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.8526435,-0.555416),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.85381033,-0.55340867),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.85411167,-0.5561055),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.85411167,-0.5561055),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.85439817,-0.555215),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.85445133,-0.5542865),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.849695,-0.5605635),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.84873933,-0.56130667),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.82439317,-0.58298467),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.856081,-0.659003),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.826333,-0.588399),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.826333,-0.588399),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.826333,-0.588399),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.825616,-0.58824233),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81358333,-0.58364217),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81358333,-0.58364217),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81358333,-0.58364217),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81358333,-0.58364217),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81358333,-0.58364217),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81358333,-0.58364217),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.81358333,-0.58364217),stopover:true});
- waypts.push({location:new google.maps.LatLng(50.797378, 0.058504),stopover:true});
- }
- function calcRoute() {
- var combinedResults;
- var unsortedResults = [{}]; // to hold the counter and the results themselves as they come back, to later sort
- var directionsResultsReturned = 0;
- var itemsPerBatch = 6; // google API max - 1 start, 1 stop, and 8 waypoints
- var itemsCounter = 0;
- var wayptsExist = waypts.length > 0;
- while (wayptsExist) {
- var subBatch = [];
- var subitemsCounter = 0;
- for (var j = itemsCounter; j < waypts.length; j++) {
- subitemsCounter++;
- subBatch.push(waypts[j]);
- if (subitemsCounter == itemsPerBatch)
- break;
- }
- itemsCounter += subitemsCounter;
- batches.push(subBatch);
- wayptsExist = itemsCounter < waypts.length;
- itemsCounter--;
- }
- for (k = 0; k < batches.length; k++) {
- var lastIndex = batches[k].length - 1;
- var start = batches[k][0].location;
- var end = batches[k][lastIndex].location;
- // trim first and last entry from array
- setTimeout(makeRequest,3000*(k+1));
- }
- }
- function makeRequest(){
- var way = [];
- way = batches[k];
- way.splice(0, 1);
- way.splice(waypts.length - 1, 1);
- var request = {
- origin: start,
- destination: end,
- waypoints: way,
- travelMode: google.maps.TravelMode.DRIVING
- };
- directionsService.route(request, function (result, status) {
- window.alert(status)
- if (status == window.google.maps.DirectionsStatus.OK) {
- var unsortedResult = { order: kk, result: result };
- unsortedResults.push(unsortedResult);
- directionsResultsReturned++;
- if (directionsResultsReturned == batches.length) // we've received all the results. put to map
- {
- // sort the returned values into their correct order
- unsortedResults.sort(function (a, b) { return parseFloat(a.order) - parseFloat(b.order); });
- var count = 0;
- for (var key in unsortedResults) {
- if (unsortedResults[key].result != null) {
- if (unsortedResults.hasOwnProperty(key)) {
- if (count == 0) // first results. new up the combinedResults object
- combinedResults = unsortedResults[key].result;
- else {
- // only building up legs, overview_path, and bounds in my consolidated object. This is not a complete
- // directionResults object, but enough to draw a path on the map, which is all I need
- combinedResults.routes[0].legs = combinedResults.routes[0].legs.concat(unsortedResults[key].result.routes[0].legs);
- combinedResults.routes[0].overview_path = combinedResults.routes[0].overview_path.concat(unsortedResults[key].result.routes[0].overview_path);
- combinedResults.routes[0].bounds = combinedResults.routes[0].bounds.extend(unsortedResults[key].result.routes[0].bounds.getNorthEast());
- combinedResults.routes[0].bounds = combinedResults.routes[0].bounds.extend(unsortedResults[key].result.routes[0].bounds.getSouthWest());
- }
- count++;
- }
- }
- }
- directionsDisplay.setDirections(combinedResults);
- var legs = combinedResults.routes[0].legs;
- // alert(legs.length);
- for (var i=0; i < legs.length;i++){
- var markerletter = "A".charCodeAt(0);
- markerletter += i;
- markerletter = String.fromCharCode(markerletter);
- createMarker(directionsDisplay.getMap(),legs[i].start_location,"marker"+i,"some text for marker "+i+"<br>"+legs[i].start_address,markerletter);
- }
- var i=legs.length;
- var markerletter = "A".charCodeAt(0);
- markerletter += i;
- markerletter = String.fromCharCode(markerletter);
- createMarker(directionsDisplay.getMap(),legs[legs.length-1].end_location,"marker"+i,"some text for the "+i+"marker<br>"+legs[legs.length-1].end_address,markerletter);
- }
- }
- });
- }
- </script>
- </head>
- <body>
- <div id="map-canvas" style="float:left;width:70%;height:100%;"></div>
- <div id="control_panel" style="float:right;width:30%;text-align:left;padding-top:20px">
- <div style="margin:20px;border-width:2px;">
- <b>Start:</b>
- <select id="start">
- <option value="Halifax, NS">Halifax, NS</option>
- <option value="Boston, MA">Boston, MA</option>
- <option value="New York, NY">New York, NY</option>
- <option value="Miami, FL">Miami, FL</option>
- </select>
- <br>
- <b>Waypoints:</b> <br>
- <i>(Ctrl-Click for multiple selection)</i> <br>
- <select multiple id="waypoints">
- <option value="montreal, quebec">Montreal, QBC</option>
- <option value="toronto, ont">Toronto, ONT</option>
- <option value="chicago, il">Chicago</option>
- <option value="winnipeg, mb">Winnipeg</option>
- <option value="fargo, nd">Fargo</option>
- <option value="calgary, ab">Calgary</option>
- <option value="spokane, wa">Spokane</option>
- </select>
- <br>
- <b>End:</b>
- <select id="end">
- <option value="Vancouver, BC">Vancouver, BC</option>
- <option value="Seattle, WA">Seattle, WA</option>
- <option value="San Francisco, CA">San Francisco, CA</option>
- <option value="Los Angeles, CA">Los Angeles, CA</option>
- </select>
- <br>
- <input type="submit" onclick="initialize(); calcRoute();">
- </div>
- <div id="directions_panel" style="margin:20px;background-color:#FFEE77;"></div>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement