Skip to content

Instantly share code, notes, and snippets.

@frah
Created October 14, 2012 17:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frah/3889213 to your computer and use it in GitHub Desktop.
Save frah/3889213 to your computer and use it in GitHub Desktop.
Route search function with more than 8 waypoints using Google Maps API v3
function searchLongRoute(waypoints) {
var route;
var unit = 10;
var unit_num = Math.ceil((waypoints.length+Math.ceil(waypoints.length/unit)-1)/unit);
for (var start_num = 0; start_num < waypoints.length-1;) {
var s = waypoints[start_num];
var next_start = ((waypoints.length>=start_num+unit)?(start_num+unit):(waypoints.length))-1;
var e = waypoints[next_start];
var w = waypoints.slice(start_num+1, next_start);
direction.route({
'origin': s.location,
'destination': e.location,
'travelMode': tMode,
'waypoints': w,
'avoidHighways': true,
'avoidTolls': true
}, function(ret, st){
if (st == google.maps.DirectionsStatus.OK) {
if (route) {
route.routes[0].legs = route.routes[0].legs.concat(ret.routes[0].legs);
route.routes[0].overview_path = route.routes[0].overview_path.concat(ret.routes[0].overview_path);
route.routes[0].bounds = route.routes[0].bounds.extend(ret.routes[0].bounds.getNorthEast());
route.routes[0].bounds = route.routes[0].bounds.extend(ret.routes[0].bounds.getSouthWest());
} else {
route = ret;
}
} else {
console.log(st);
}
if (!--unit_num) {
renderer.setMap(map);
renderer.setDirections(route);
}
});
start_num = next_start;
}
}
var direction = new google.maps.DirectionsService();
var renderer = new google.maps.DirectionsRenderer();
@iksik
Copy link

iksik commented Oct 25, 2012

Hey frah,

I looking few days for a solution to handle more then 8 waypoints with no result :( Can You provide more info how to use Your funcion. I will be really thankfull for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment