Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* WITHIN JQUERY success: function() */
- var postcodes = jQuery.parseJSON(data2);
- var extraSkip = 0;
- var skipCounter = [];
- if ( postcodes.length > 10 )
- {
- // split and rebuild long routes
- var batches = [];
- var itemsPerBatch = 8; // google API max - 1 start, 1 stop, and 8 waypoints
- var itemsCounter = 0;
- var wayptsExist = postcodes.length > 0;
- var skipCounter = [];
- while (wayptsExist) {
- var subBatch = [];
- var subitemsCounter = 0;
- for (var j = itemsCounter; j < postcodes.length; j++) {
- var skipThisPoint = false;
- if (postcodes[j] == 'noplot')
- {
- // use the previous location's postcode for the non-plottable waypt
- skipThisPoint = true;
- skipCounter.push(j);
- }
- else if (postcodes[j] == 'skip')
- {
- // completely skip this point, but still increment number
- skipThisPoint = true;
- skipCounter.push(j);
- }
- else
- {
- postcodeToUse = postcodes[j];
- }
- subitemsCounter++;
- if (!skipThisPoint)
- {
- subBatch.push({
- location: postcodeToUse,
- stopover: true
- });
- }
- if (subitemsCounter == itemsPerBatch) break;
- }
- itemsCounter += subitemsCounter;
- batches.push(subBatch);
- wayptsExist = itemsCounter < postcodes.length;
- }
- // START OF MARKER MAPPING
- routeMapping.calcLongRoute(batches, directionsService, directionsDisplay,
- function(route)
- {
- var totalMilage = 0;
- var thisMilage = '';
- if (route.legs[0].start_address != route.legs[route.legs.length-1].end_address)
- {
- routeMapping.makeMarker( route.legs[0].start_location, routeMapping.iconWaypointDepot('C0FFC0'), 'Depot (Departure)' );
- routeMapping.makeMarker( route.legs[route.legs.length-1].end_location, routeMapping.iconWaypointDepot('FFC0C0'), 'Depot (Return)' );
- }
- else
- {
- routeMapping.makeMarker( route.legs[0].start_location, routeMapping.iconWaypointDepot('A3D7EF'), 'Depot (Depart/Return)' );
- }
- for (i = 0; i < route.legs.length; i++ ) {
- // account for missing legs. not the paraplegic kind, the non-plottable kind.
- j = i;
- for (x = 0; x < skipCounter.length; x++)
- {
- if ( i == skipCounter[x] - 1 )
- {
- j = j + 1;
- extraSkip++;
- }
- else
- {
- j = j + extraSkip;
- }
- }
- var isLastLeg = false;
- var isFirstLeg = false;
- if (i == (route.legs.length - 1)) isLastLeg = true;
- if (i == 0) isFirstLeg = true;
- if (!isLastLeg)
- {
- if (!isFirstLeg)
- {
- if ( (route.legs[i-1].end_location == route.legs[i].end_location) )
- {
- // this leg ends up in the same location as another leg, so attempt to alter colour.
- routeMapping.makeMarker( route.legs[i].end_location, routeMapping.iconWaypointNum(j+1, 'DC5C00'), route.legs[i].end_address );
- }
- else
- {
- routeMapping.makeMarker( route.legs[i].end_location, routeMapping.iconWaypointNum(j+1), route.legs[i].end_address );
- }
- }
- else
- {
- routeMapping.makeMarker( route.legs[i].end_location, routeMapping.iconWaypointNum(j+1), route.legs[i].end_address );
- }
- }
- $j('.legDistance' + i).html( route.legs[i].distance.text );
- thisMilage = route.legs[i].distance.text;
- var isInFeet = (thisMilage.indexOf("ft") != -1) ? true : false;
- if (isInFeet)
- {
- thisMilage = thisMilage.replace(' ft', '');
- var milageFloat = parseFloat(thisMilage);
- milageFloat = milageFloat * 0.000189393939; // 1 foot = 0.000189393939 miles
- }
- else
- {
- thisMilage = thisMilage.replace(' mi', '');
- var milageFloat = parseFloat(thisMilage);
- }
- totalMilage = totalMilage + milageFloat;
- }
- // update milage
- var originalMilage = $j('.runSelected').children('td:last').prev().prev().prev().html();
- $j('.runSelected').children('td:last').prev().prev().prev().html(totalMilage.toFixed(2) + ' mi');
- $j('.totalMilageCell').children('strong').html(totalMilage.toFixed(2) + ' mi');
- if ( originalMilage != totalMilage + ' mi' )
- {
- $j.ajax(
- {
- type: 'get',
- async: true,
- url: '/runAllocation/setTotalMilageForRun',
- data: 'mi=' + totalMilage + '&rh=' + clickedLink.prev('.hiddenAnchor').attr('name'),
- cache: false
- }
- );
- }
- // plot current driver location - only doing this on 'initial' lookup
- // as there's no point to doing this everytime the route is updated
- $j.ajax(
- {
- type: 'get',
- async: true,
- url: '/runAllocation/getCurrentDriverLocation',
- data: 'run=' + clickedLink.prev('.hiddenAnchor').attr('name'),
- cache: false,
- success: function (driverLocationData)
- {
- var result = jQuery.parseJSON(driverLocationData);
- if (result.success && result.location != '')
- {
- var locationCoords = new google.maps.LatLng(result.lat,result.lng);
- routeMapping.makeMarker( locationCoords, routeMapping.iconWaypointDriver(), 'Approximate location of ' + result.driverName );
- }
- }
- }
- );
- }
- );
- // END OF MARKER MAPPING
- }
- /* Route Calc object - there's more to this than pasted here */
- routeMapping = {
- 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
Advertisement