Created
October 14, 2012 17:12
-
-
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
This file contains 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
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.