Created
April 15, 2014 05:52
-
-
Save mathewka/10705571 to your computer and use it in GitHub Desktop.
Google map Get direction api 8 way point limit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| calcRoute : function (batches, directionsService, directionsDisplay) { | |
| var combinedResults; | |
| var unsortedResults = [{}]; // to hold the counter and the results themselves as they come back, to later sort | |
| var directionsResultsReturned = 0; | |
| for (var 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 | |
| var waypts = []; | |
| waypts = batches[k]; | |
| waypts.splice(0, 1); | |
| waypts.splice(waypts.length - 1, 1); | |
| var request = { | |
| origin : start, | |
| destination : end, | |
| waypoints : waypts, | |
| travelMode : window.google.maps.TravelMode.WALKING | |
| }; | |
| (function (kk) { | |
| directionsService.route(request, function (result, 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); | |
| } | |
| } | |
| }); | |
| })(k); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
reference
http://lemonharpy.wordpress.com/2011/12/15/working-around-8-waypoint-limit-in-google-maps-directions-api/