Guest User

multi-batch route mapping

a guest
Feb 29th, 2012
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. calcLongRoute: function (batches, directionsService, directionsDisplay, callback)
  2.     {
  3.         var combinedResults;
  4.         var directionsResultsReturned = 0;
  5.        
  6.         var totalCombinedResults = [];
  7.         var startEndRefArray = [];
  8.        
  9.        
  10.         // make a reference to start and ending so that we can compare it to the passed-through 'first and last' waypts
  11.         var nextBatchStartPoint = [];
  12.         for (var b = 0; b < batches.length; b++) {
  13.  
  14.             var lastIndex = batches[b].length - 1;
  15.             var pointHelper = [];
  16.            
  17.             if (b == 0)
  18.             {
  19.                 var start = batches[b][0].location;
  20.                 nextBatchStartPoint[b+1] = batches[b][lastIndex].location
  21.             }
  22.             else
  23.             {
  24.                 var start = nextBatchStartPoint[b];
  25.                 nextBatchStartPoint[b+1] = batches[b][lastIndex].location
  26.             }
  27.            
  28.             pointHelper.push(b, start, batches[b][lastIndex].location);
  29.            
  30.             startEndRefArray.push(pointHelper);
  31.         }
  32.  
  33.        
  34.         // now loop through points again, and
  35.         var nextBatchStartPoint = [];
  36.         for (var k = 0; k < batches.length; k++) {
  37.                        
  38.             var lastIndex = batches[k].length - 1;
  39.             var waypts = [];
  40.            
  41.             waypts = batches[k];
  42.            
  43.             if (k == 0)
  44.             {
  45.                 var start = batches[k][0].location;
  46.                 nextBatchStartPoint[k+1] = batches[k][lastIndex].location
  47.             }
  48.             else
  49.             {
  50.                 var start = nextBatchStartPoint[k];
  51.                 nextBatchStartPoint[k+1] = batches[k][lastIndex].location
  52.             }
  53.            
  54.            
  55.             var end = batches[k][lastIndex].location;
  56.            
  57.             //if this is the VERY first item (i.e. the starting depot) we want to remove it to remove duplicates on map
  58.             if (k == 0)
  59.             {
  60.                 waypts.splice(0, 1);
  61.             }
  62.            
  63.             // trim last entry from array
  64.             waypts.splice(waypts.length - 1, 1);
  65.  
  66.             var request = {
  67.                 origin: start,
  68.                 destination: end,
  69.                 waypoints: waypts,
  70.                 optimizeWaypoints: false,
  71.                 unitSystem: google.maps.UnitSystem.IMPERIAL,
  72.                 travelMode: google.maps.DirectionsTravelMode.DRIVING
  73.             };
  74.            
  75.             // keeping nasty commented code in here in case things go wrong
  76.             directionsService.route(request, function (result, status)
  77.             {
  78.                 if (status == window.google.maps.DirectionsStatus.OK)
  79.                 {
  80.  
  81.                     // check if expected results are found before looping through all this and getting ugly errors.
  82.                     // Google appears to be changing the variable name passed through the results. routes and status
  83.                     // are fine, but the 'original request' one has changed 3 times now. So this should accommodate
  84.                     // changes to this.
  85.  
  86.                     //var passedRequest = result.Zf; // pass-through of original request object
  87.                     var passoverArray = ['routes','status']; // keys that are allowed to be skipped.
  88.                     var extraKeyCount = 0;
  89.                     var passedRequest;
  90.                     for (key in result)
  91.                     {                        
  92.                         if ( key in tools.isInArray(passoverArray) )
  93.                         {
  94.                             // do nothing
  95.                         }
  96.                         else
  97.                         {
  98.                             extraKeyCount++;
  99.                             passedRequest = result[key];
  100.                         }
  101.                        
  102.                     }
  103.                    
  104.                     if (extraKeyCount == 1) // there should only be one left. If Google adds more, we need to look
  105.                     {                       // so this should error out gracefully if this happens
  106.                         // do nothing, because we can continue
  107.                     }
  108.                     else if (extraKeyCount > 1) // will not happen unless Google adds another object
  109.                     {
  110.                         alert ('There appears to be an error with the route, which could not be calculated. The error code was "TOO_MANY_RESULT_OBJECTS".');
  111.                         return false;
  112.                     }
  113.                     else // should not happen ever, unless google completely removes  all variables other than those passed over
  114.                     {
  115.                         alert ('There appears to be an error with the route, which could not be calculated. The error code was "KEY_COUNT_IS_ZERO".');
  116.                         return false;
  117.                     }
  118.                    
  119.                    
  120.                    
  121.                     if (directionsResultsReturned == 0) { // first bunch of results in. new up the combinedResults object
  122.                         combinedResults = result;
  123.                        
  124.                         for (b = 0; b < startEndRefArray.length; b++)
  125.                         {
  126.  
  127.                             if ( passedRequest.origin == startEndRefArray[b][1] && passedRequest.destination == startEndRefArray[b][2])
  128.                             {
  129.  
  130.                                 totalCombinedResults[startEndRefArray[b][0]] = result;
  131.                                 directionsResultsReturned++;
  132.                                
  133.                             }
  134.                             else
  135.                             {
  136.                                 // no match, so do nothing
  137.                             }
  138.                            
  139.                         }
  140.                        
  141.                     }
  142.                     else
  143.                     {
  144.                         // only building up legs, overview_path, and bounds in my consolidated object. This is not a complete
  145.                         // directionResults object, but enough to draw a path on the map, which is all we need
  146.                         for (b = 0; b < startEndRefArray.length; b++)
  147.                         {
  148.  
  149.                             if ( passedRequest.origin == startEndRefArray[b][1] && passedRequest.destination == startEndRefArray[b][2])
  150.                             {
  151.                                
  152.                                 totalCombinedResults[startEndRefArray[b][0]] = result;
  153.                                 directionsResultsReturned++;
  154.                                
  155.                             }
  156.                             else
  157.                             {
  158.                                 // no match, so do nothing
  159.                             }
  160.                            
  161.                         }
  162.  
  163.                     }
  164.                                  
  165.                    
  166.                    
  167.                     if (directionsResultsReturned == batches.length)
  168.                     {
  169.                        
  170.                         // now we're done building results, so sort and extract the 'original' combined results.
  171.                         var combinedResultsResorted = [];
  172.                         var combinedResultsResortedTemp = [];
  173.                        
  174.                         var combinedResultsFinal;
  175.                         for (t = 0; t < totalCombinedResults.length; t++)
  176.                         {
  177.                            
  178.                             if (t == 0)
  179.                             {
  180.                                 combinedResultsFinal = totalCombinedResults[t];
  181.                             }
  182.                             else
  183.                             {
  184.                                 var previousRoutes = totalCombinedResults[t].routes[0];
  185.                                
  186.                                 combinedResultsFinal.routes[0].legs = combinedResultsFinal.routes[0].legs.concat( totalCombinedResults[t].routes[0].legs );
  187.                                 combinedResultsFinal.routes[0].overview_path = combinedResultsFinal.routes[0].overview_path.concat( totalCombinedResults[t].routes[0].overview_path );
  188.                             }
  189.                            
  190.                         }
  191.                        
  192.                         directionsDisplay.setDirections(combinedResultsFinal);
  193.                         callback(combinedResultsFinal.routes[0]);
  194.                        
  195.                     }
  196.                    
  197.                    
  198.                 }
  199.                 else
  200.                 {
  201.                     alert ('Unfortunately there was an error plotting your map. We\'re sorry about that! The error returned was ' + status);
  202.                 }
  203.                
  204.             });
  205.                    
  206.  
  207.         }
  208.        
  209.        
  210.     }
Advertisement
Add Comment
Please, Sign In to add comment