SHOW:
|
|
- or go back to the newest paste.
| 1 | /* WITHIN JQUERY success: function() */ | |
| 2 | var postcodes = jQuery.parseJSON(data2); | |
| 3 | var extraSkip = 0; | |
| 4 | var skipCounter = []; | |
| 5 | if ( postcodes.length > 10 ) | |
| 6 | {
| |
| 7 | ||
| 8 | // split and rebuild long routes | |
| 9 | var batches = []; | |
| 10 | var itemsPerBatch = 8; // google API max - 1 start, 1 stop, and 8 waypoints | |
| 11 | var itemsCounter = 0; | |
| 12 | var wayptsExist = postcodes.length > 0; | |
| 13 | var skipCounter = []; | |
| 14 | while (wayptsExist) {
| |
| 15 | var subBatch = []; | |
| 16 | var subitemsCounter = 0; | |
| 17 | ||
| 18 | for (var j = itemsCounter; j < postcodes.length; j++) {
| |
| 19 | ||
| 20 | var skipThisPoint = false; | |
| 21 | if (postcodes[j] == 'noplot') | |
| 22 | {
| |
| 23 | // use the previous location's postcode for the non-plottable waypt | |
| 24 | skipThisPoint = true; | |
| 25 | skipCounter.push(j); | |
| 26 | } | |
| 27 | else if (postcodes[j] == 'skip') | |
| 28 | {
| |
| 29 | // completely skip this point, but still increment number | |
| 30 | skipThisPoint = true; | |
| 31 | skipCounter.push(j); | |
| 32 | ||
| 33 | } | |
| 34 | else | |
| 35 | {
| |
| 36 | postcodeToUse = postcodes[j]; | |
| 37 | } | |
| 38 | ||
| 39 | ||
| 40 | subitemsCounter++; | |
| 41 | ||
| 42 | if (!skipThisPoint) | |
| 43 | {
| |
| 44 | subBatch.push({
| |
| 45 | location: postcodeToUse, | |
| 46 | stopover: true | |
| 47 | }); | |
| 48 | } | |
| 49 | ||
| 50 | if (subitemsCounter == itemsPerBatch) break; | |
| 51 | ||
| 52 | } | |
| 53 | ||
| 54 | itemsCounter += subitemsCounter; | |
| 55 | batches.push(subBatch); | |
| 56 | wayptsExist = itemsCounter < postcodes.length; | |
| 57 | ||
| 58 | } | |
| 59 | ||
| 60 | ||
| 61 | // START OF MARKER MAPPING | |
| 62 | routeMapping.calcLongRoute(batches, directionsService, directionsDisplay, | |
| 63 | function(route) | |
| 64 | {
| |
| 65 | ||
| 66 | var totalMilage = 0; | |
| 67 | var thisMilage = ''; | |
| 68 | ||
| 69 | if (route.legs[0].start_address != route.legs[route.legs.length-1].end_address) | |
| 70 | {
| |
| 71 | routeMapping.makeMarker( route.legs[0].start_location, routeMapping.iconWaypointDepot('C0FFC0'), 'Depot (Departure)' );
| |
| 72 | routeMapping.makeMarker( route.legs[route.legs.length-1].end_location, routeMapping.iconWaypointDepot('FFC0C0'), 'Depot (Return)' );
| |
| 73 | } | |
| 74 | else | |
| 75 | {
| |
| 76 | routeMapping.makeMarker( route.legs[0].start_location, routeMapping.iconWaypointDepot('A3D7EF'), 'Depot (Depart/Return)' );
| |
| 77 | } | |
| 78 | ||
| 79 | for (i = 0; i < route.legs.length; i++ ) {
| |
| 80 | ||
| 81 | // account for missing legs. not the paraplegic kind, the non-plottable kind. | |
| 82 | j = i; | |
| 83 | for (x = 0; x < skipCounter.length; x++) | |
| 84 | {
| |
| 85 | if ( i == skipCounter[x] - 1 ) | |
| 86 | {
| |
| 87 | j = j + 1; | |
| 88 | extraSkip++; | |
| 89 | } | |
| 90 | else | |
| 91 | {
| |
| 92 | j = j + extraSkip; | |
| 93 | } | |
| 94 | } | |
| 95 | ||
| 96 | var isLastLeg = false; | |
| 97 | var isFirstLeg = false; | |
| 98 | if (i == (route.legs.length - 1)) isLastLeg = true; | |
| 99 | if (i == 0) isFirstLeg = true; | |
| 100 | ||
| 101 | ||
| 102 | if (!isLastLeg) | |
| 103 | {
| |
| 104 | if (!isFirstLeg) | |
| 105 | {
| |
| 106 | if ( (route.legs[i-1].end_location == route.legs[i].end_location) ) | |
| 107 | {
| |
| 108 | // this leg ends up in the same location as another leg, so attempt to alter colour. | |
| 109 | routeMapping.makeMarker( route.legs[i].end_location, routeMapping.iconWaypointNum(j+1, 'DC5C00'), route.legs[i].end_address ); | |
| 110 | } | |
| 111 | else | |
| 112 | {
| |
| 113 | routeMapping.makeMarker( route.legs[i].end_location, routeMapping.iconWaypointNum(j+1), route.legs[i].end_address ); | |
| 114 | } | |
| 115 | } | |
| 116 | else | |
| 117 | {
| |
| 118 | routeMapping.makeMarker( route.legs[i].end_location, routeMapping.iconWaypointNum(j+1), route.legs[i].end_address ); | |
| 119 | } | |
| 120 | } | |
| 121 | ||
| 122 | $j('.legDistance' + i).html( route.legs[i].distance.text );
| |
| 123 | ||
| 124 | ||
| 125 | thisMilage = route.legs[i].distance.text; | |
| 126 | var isInFeet = (thisMilage.indexOf("ft") != -1) ? true : false;
| |
| 127 | if (isInFeet) | |
| 128 | {
| |
| 129 | thisMilage = thisMilage.replace(' ft', '');
| |
| 130 | var milageFloat = parseFloat(thisMilage); | |
| 131 | milageFloat = milageFloat * 0.000189393939; // 1 foot = 0.000189393939 miles | |
| 132 | } | |
| 133 | else | |
| 134 | {
| |
| 135 | thisMilage = thisMilage.replace(' mi', '');
| |
| 136 | var milageFloat = parseFloat(thisMilage); | |
| 137 | } | |
| 138 | ||
| 139 | totalMilage = totalMilage + milageFloat; | |
| 140 | ||
| 141 | } | |
| 142 | ||
| 143 | // update milage | |
| 144 | var originalMilage = $j('.runSelected').children('td:last').prev().prev().prev().html();
| |
| 145 | $j('.runSelected').children('td:last').prev().prev().prev().html(totalMilage.toFixed(2) + ' mi');
| |
| 146 | $j('.totalMilageCell').children('strong').html(totalMilage.toFixed(2) + ' mi');
| |
| 147 | ||
| 148 | if ( originalMilage != totalMilage + ' mi' ) | |
| 149 | {
| |
| 150 | $j.ajax( | |
| 151 | {
| |
| 152 | type: 'get', | |
| 153 | async: true, | |
| 154 | url: '/runAllocation/setTotalMilageForRun', | |
| 155 | data: 'mi=' + totalMilage + '&rh=' + clickedLink.prev('.hiddenAnchor').attr('name'),
| |
| 156 | cache: false | |
| 157 | } | |
| 158 | ); | |
| 159 | } | |
| 160 | ||
| 161 | // plot current driver location - only doing this on 'initial' lookup | |
| 162 | // as there's no point to doing this everytime the route is updated | |
| 163 | $j.ajax( | |
| 164 | {
| |
| 165 | type: 'get', | |
| 166 | async: true, | |
| 167 | url: '/runAllocation/getCurrentDriverLocation', | |
| 168 | data: 'run=' + clickedLink.prev('.hiddenAnchor').attr('name'),
| |
| 169 | cache: false, | |
| 170 | success: function (driverLocationData) | |
| 171 | {
| |
| 172 | ||
| 173 | var result = jQuery.parseJSON(driverLocationData); | |
| 174 | ||
| 175 | if (result.success && result.location != '') | |
| 176 | {
| |
| 177 | var locationCoords = new google.maps.LatLng(result.lat,result.lng); | |
| 178 | routeMapping.makeMarker( locationCoords, routeMapping.iconWaypointDriver(), 'Approximate location of ' + result.driverName ); | |
| 179 | } | |
| 180 | ||
| 181 | } | |
| 182 | } | |
| 183 | ); | |
| 184 | ||
| 185 | } | |
| 186 | ); | |
| 187 | // END OF MARKER MAPPING | |
| 188 | ||
| 189 | } | |
| 190 | ||
| 191 | /* Route Calc object - there's more to this than pasted here */ | |
| 192 | routeMapping = {
| |
| 193 | calcLongRoute: function (batches, directionsService, directionsDisplay, callback) | |
| 194 | {
| |
| 195 | var combinedResults; | |
| 196 | var directionsResultsReturned = 0; | |
| 197 | ||
| 198 | var totalCombinedResults = []; | |
| 199 | var startEndRefArray = []; | |
| 200 | ||
| 201 | ||
| 202 | // make a reference to start and ending so that we can compare it to the passed-through 'first and last' waypts | |
| 203 | var nextBatchStartPoint = []; | |
| 204 | for (var b = 0; b < batches.length; b++) {
| |
| 205 | ||
| 206 | var lastIndex = batches[b].length - 1; | |
| 207 | var pointHelper = []; | |
| 208 | ||
| 209 | if (b == 0) | |
| 210 | - | } |
| 210 | + | |
| 211 | var start = batches[b][0].location; | |
| 212 | nextBatchStartPoint[b+1] = batches[b][lastIndex].location | |
| 213 | } | |
| 214 | else | |
| 215 | {
| |
| 216 | var start = nextBatchStartPoint[b]; | |
| 217 | nextBatchStartPoint[b+1] = batches[b][lastIndex].location | |
| 218 | } | |
| 219 | ||
| 220 | pointHelper.push(b, start, batches[b][lastIndex].location); | |
| 221 | ||
| 222 | startEndRefArray.push(pointHelper); | |
| 223 | } | |
| 224 | ||
| 225 | ||
| 226 | // now loop through points again, and | |
| 227 | var nextBatchStartPoint = []; | |
| 228 | for (var k = 0; k < batches.length; k++) {
| |
| 229 | ||
| 230 | var lastIndex = batches[k].length - 1; | |
| 231 | var waypts = []; | |
| 232 | ||
| 233 | waypts = batches[k]; | |
| 234 | ||
| 235 | if (k == 0) | |
| 236 | {
| |
| 237 | var start = batches[k][0].location; | |
| 238 | nextBatchStartPoint[k+1] = batches[k][lastIndex].location | |
| 239 | } | |
| 240 | else | |
| 241 | {
| |
| 242 | var start = nextBatchStartPoint[k]; | |
| 243 | nextBatchStartPoint[k+1] = batches[k][lastIndex].location | |
| 244 | } | |
| 245 | ||
| 246 | ||
| 247 | var end = batches[k][lastIndex].location; | |
| 248 | ||
| 249 | //if this is the VERY first item (i.e. the starting depot) we want to remove it to remove duplicates on map | |
| 250 | if (k == 0) | |
| 251 | {
| |
| 252 | waypts.splice(0, 1); | |
| 253 | } | |
| 254 | ||
| 255 | // trim last entry from array | |
| 256 | waypts.splice(waypts.length - 1, 1); | |
| 257 | ||
| 258 | var request = {
| |
| 259 | origin: start, | |
| 260 | destination: end, | |
| 261 | waypoints: waypts, | |
| 262 | optimizeWaypoints: false, | |
| 263 | unitSystem: google.maps.UnitSystem.IMPERIAL, | |
| 264 | travelMode: google.maps.DirectionsTravelMode.DRIVING | |
| 265 | }; | |
| 266 | ||
| 267 | // keeping nasty commented code in here in case things go wrong | |
| 268 | directionsService.route(request, function (result, status) | |
| 269 | {
| |
| 270 | if (status == window.google.maps.DirectionsStatus.OK) | |
| 271 | {
| |
| 272 | ||
| 273 | // check if expected results are found before looping through all this and getting ugly errors. | |
| 274 | // Google appears to be changing the variable name passed through the results. routes and status | |
| 275 | // are fine, but the 'original request' one has changed 3 times now. So this should accommodate | |
| 276 | // changes to this. | |
| 277 | ||
| 278 | //var passedRequest = result.Zf; // pass-through of original request object | |
| 279 | var passoverArray = ['routes','status']; // keys that are allowed to be skipped. | |
| 280 | var extraKeyCount = 0; | |
| 281 | var passedRequest; | |
| 282 | for (key in result) | |
| 283 | {
| |
| 284 | if ( key in tools.isInArray(passoverArray) ) | |
| 285 | {
| |
| 286 | // do nothing | |
| 287 | } | |
| 288 | else | |
| 289 | {
| |
| 290 | extraKeyCount++; | |
| 291 | passedRequest = result[key]; | |
| 292 | } | |
| 293 | ||
| 294 | } | |
| 295 | ||
| 296 | if (extraKeyCount == 1) // there should only be one left. If Google adds more, we need to look | |
| 297 | { // so this should error out gracefully if this happens
| |
| 298 | // do nothing, because we can continue | |
| 299 | } | |
| 300 | else if (extraKeyCount > 1) // will not happen unless Google adds another object | |
| 301 | {
| |
| 302 | alert ('There appears to be an error with the route, which could not be calculated. The error code was "TOO_MANY_RESULT_OBJECTS".');
| |
| 303 | return false; | |
| 304 | } | |
| 305 | else // should not happen ever, unless google completely removes all variables other than those passed over | |
| 306 | {
| |
| 307 | alert ('There appears to be an error with the route, which could not be calculated. The error code was "KEY_COUNT_IS_ZERO".');
| |
| 308 | return false; | |
| 309 | } | |
| 310 | ||
| 311 | ||
| 312 | ||
| 313 | if (directionsResultsReturned == 0) { // first bunch of results in. new up the combinedResults object
| |
| 314 | combinedResults = result; | |
| 315 | ||
| 316 | for (b = 0; b < startEndRefArray.length; b++) | |
| 317 | {
| |
| 318 | ||
| 319 | if ( passedRequest.origin == startEndRefArray[b][1] && passedRequest.destination == startEndRefArray[b][2]) | |
| 320 | {
| |
| 321 | ||
| 322 | totalCombinedResults[startEndRefArray[b][0]] = result; | |
| 323 | directionsResultsReturned++; | |
| 324 | ||
| 325 | } | |
| 326 | else | |
| 327 | {
| |
| 328 | // no match, so do nothing | |
| 329 | } | |
| 330 | ||
| 331 | } | |
| 332 | ||
| 333 | } | |
| 334 | else | |
| 335 | {
| |
| 336 | // only building up legs, overview_path, and bounds in my consolidated object. This is not a complete | |
| 337 | // directionResults object, but enough to draw a path on the map, which is all we need | |
| 338 | for (b = 0; b < startEndRefArray.length; b++) | |
| 339 | {
| |
| 340 | ||
| 341 | if ( passedRequest.origin == startEndRefArray[b][1] && passedRequest.destination == startEndRefArray[b][2]) | |
| 342 | {
| |
| 343 | ||
| 344 | totalCombinedResults[startEndRefArray[b][0]] = result; | |
| 345 | directionsResultsReturned++; | |
| 346 | ||
| 347 | } | |
| 348 | else | |
| 349 | {
| |
| 350 | // no match, so do nothing | |
| 351 | } | |
| 352 | ||
| 353 | } | |
| 354 | ||
| 355 | } | |
| 356 | ||
| 357 | ||
| 358 | ||
| 359 | if (directionsResultsReturned == batches.length) | |
| 360 | {
| |
| 361 | ||
| 362 | // now we're done building results, so sort and extract the 'original' combined results. | |
| 363 | var combinedResultsResorted = []; | |
| 364 | var combinedResultsResortedTemp = []; | |
| 365 | ||
| 366 | var combinedResultsFinal; | |
| 367 | for (t = 0; t < totalCombinedResults.length; t++) | |
| 368 | {
| |
| 369 | ||
| 370 | if (t == 0) | |
| 371 | {
| |
| 372 | combinedResultsFinal = totalCombinedResults[t]; | |
| 373 | } | |
| 374 | else | |
| 375 | {
| |
| 376 | var previousRoutes = totalCombinedResults[t].routes[0]; | |
| 377 | ||
| 378 | combinedResultsFinal.routes[0].legs = combinedResultsFinal.routes[0].legs.concat( totalCombinedResults[t].routes[0].legs ); | |
| 379 | combinedResultsFinal.routes[0].overview_path = combinedResultsFinal.routes[0].overview_path.concat( totalCombinedResults[t].routes[0].overview_path ); | |
| 380 | } | |
| 381 | ||
| 382 | } | |
| 383 | ||
| 384 | directionsDisplay.setDirections(combinedResultsFinal); | |
| 385 | callback(combinedResultsFinal.routes[0]); | |
| 386 | ||
| 387 | } | |
| 388 | ||
| 389 | ||
| 390 | } | |
| 391 | else | |
| 392 | {
| |
| 393 | alert ('Unfortunately there was an error plotting your map. We\'re sorry about that! The error returned was ' + status);
| |
| 394 | } | |
| 395 | ||
| 396 | }); | |
| 397 | ||
| 398 | ||
| 399 | } | |
| 400 | ||
| 401 | ||
| 402 | } | |
| 403 | } |