Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- calcLongRoute: function (batches, directionsService, directionsDisplay, callback)
- {
- var combinedResults;
- var directionsResultsReturned = 0;
- var totalCombinedResults = [];
- var startEndRefArray = [];
- // make a reference to start and ending so that we can compare it to the passed-through 'first and last' waypts
- var nextBatchStartPoint = [];
- for (var b = 0; b < batches.length; b++) {
- var lastIndex = batches[b].length - 1;
- var pointHelper = [];
- if (b == 0)
- {
- var start = batches[b][0].location;
- nextBatchStartPoint[b+1] = batches[b][lastIndex].location
- }
- else
- {
- var start = nextBatchStartPoint[b];
- nextBatchStartPoint[b+1] = batches[b][lastIndex].location
- }
- pointHelper.push(b, start, batches[b][lastIndex].location);
- startEndRefArray.push(pointHelper);
- }
- // now loop through points again, and
- var nextBatchStartPoint = [];
- for (var k = 0; k < batches.length; k++) {
- var lastIndex = batches[k].length - 1;
- var waypts = [];
- waypts = batches[k];
- if (k == 0)
- {
- var start = batches[k][0].location;
- nextBatchStartPoint[k+1] = batches[k][lastIndex].location
- }
- else
- {
- var start = nextBatchStartPoint[k];
- nextBatchStartPoint[k+1] = batches[k][lastIndex].location
- }
- var end = batches[k][lastIndex].location;
- //if this is the VERY first item (i.e. the starting depot) we want to remove it to remove duplicates on map
- if (k == 0)
- {
- waypts.splice(0, 1);
- }
- // trim last entry from array
- waypts.splice(waypts.length - 1, 1);
- var request = {
- origin: start,
- destination: end,
- waypoints: waypts,
- optimizeWaypoints: false,
- unitSystem: google.maps.UnitSystem.IMPERIAL,
- travelMode: google.maps.DirectionsTravelMode.DRIVING
- };
- // keeping nasty commented code in here in case things go wrong
- directionsService.route(request, function (result, status)
- {
- if (status == window.google.maps.DirectionsStatus.OK)
- {
- // check if expected results are found before looping through all this and getting ugly errors.
- // Google appears to be changing the variable name passed through the results. routes and status
- // are fine, but the 'original request' one has changed 3 times now. So this should accommodate
- // changes to this.
- //var passedRequest = result.Zf; // pass-through of original request object
- var passoverArray = ['routes','status']; // keys that are allowed to be skipped.
- var extraKeyCount = 0;
- var passedRequest;
- for (key in result)
- {
- if ( key in tools.isInArray(passoverArray) )
- {
- // do nothing
- }
- else
- {
- extraKeyCount++;
- passedRequest = result[key];
- }
- }
- if (extraKeyCount == 1) // there should only be one left. If Google adds more, we need to look
- { // so this should error out gracefully if this happens
- // do nothing, because we can continue
- }
- else if (extraKeyCount > 1) // will not happen unless Google adds another object
- {
- alert ('There appears to be an error with the route, which could not be calculated. The error code was "TOO_MANY_RESULT_OBJECTS".');
- return false;
- }
- else // should not happen ever, unless google completely removes all variables other than those passed over
- {
- alert ('There appears to be an error with the route, which could not be calculated. The error code was "KEY_COUNT_IS_ZERO".');
- return false;
- }
- if (directionsResultsReturned == 0) { // first bunch of results in. new up the combinedResults object
- combinedResults = result;
- for (b = 0; b < startEndRefArray.length; b++)
- {
- if ( passedRequest.origin == startEndRefArray[b][1] && passedRequest.destination == startEndRefArray[b][2])
- {
- totalCombinedResults[startEndRefArray[b][0]] = result;
- directionsResultsReturned++;
- }
- else
- {
- // no match, so do nothing
- }
- }
- }
- 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 we need
- for (b = 0; b < startEndRefArray.length; b++)
- {
- if ( passedRequest.origin == startEndRefArray[b][1] && passedRequest.destination == startEndRefArray[b][2])
- {
- totalCombinedResults[startEndRefArray[b][0]] = result;
- directionsResultsReturned++;
- }
- else
- {
- // no match, so do nothing
- }
- }
- }
- if (directionsResultsReturned == batches.length)
- {
- // now we're done building results, so sort and extract the 'original' combined results.
- var combinedResultsResorted = [];
- var combinedResultsResortedTemp = [];
- var combinedResultsFinal;
- for (t = 0; t < totalCombinedResults.length; t++)
- {
- if (t == 0)
- {
- combinedResultsFinal = totalCombinedResults[t];
- }
- else
- {
- var previousRoutes = totalCombinedResults[t].routes[0];
- combinedResultsFinal.routes[0].legs = combinedResultsFinal.routes[0].legs.concat( totalCombinedResults[t].routes[0].legs );
- combinedResultsFinal.routes[0].overview_path = combinedResultsFinal.routes[0].overview_path.concat( totalCombinedResults[t].routes[0].overview_path );
- }
- }
- directionsDisplay.setDirections(combinedResultsFinal);
- callback(combinedResultsFinal.routes[0]);
- }
- }
- else
- {
- alert ('Unfortunately there was an error plotting your map. We\'re sorry about that! The error returned was ' + status);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment