1 var producers = new Array();
2 var consumers = new Array();
4 /* The URL encoding changes all of our double quotes to ", so we
5 * have to convert them back before we do anything with the JSON
7 var locations_json = "${c.json}";
8 locations_json = locations_json.replace(/"/g, '"');
9 var locations = JSON.parse(locations_json);
11 for (var location_idx = 0; location_idx < locations.length; location_idx++) {
12 if (locations[location_idx].type == 'producer') {
13 producers.push(locations[location_idx]);
16 consumers.push(locations[location_idx]);
21 // Start another JSON array, this time containing our route data.
22 var json_string = '[';
23 var completed_requests = 0;
24 var total_requests = (producers.length * consumers.length);
26 function routing_callback(result, status) {
27 /* This is the callback that gets applied to each
28 * DirectionsResult. It occurs asynchronously, which is why we keep
29 * track of the total/completed requests. Only the last call to
30 * complete should actually do something with the final result. */
31 if (status == google.maps.DirectionsStatus.OK) {
32 json_string += JSON.stringify(result);
34 for (var trip_idx = 0; trip_idx < result.trips.length; trip_idx++) {
35 num_routes = result.trips[trip_idx].routes.length;
37 alert("Unexpected number of routes (" +
38 result.trips.routes.length.toString() +
39 ") on trip number " + trip_idx.toString() + '.');
44 alert("Error, status: " + status);
47 completed_requests += 1;
49 if (completed_requests == total_requests) {
50 // Close the array, and do something with the result.
53 F.post_data('/directions/json_to_kml', json_string);
56 // Continue the array.
64 function get_directions(pairs_to_skip) {
65 /* We can only get directions for 10 producer/consumer pairs at a
66 * time. We pass the pairs_to_skip variable back to this function
67 * via setTimeout so that we can skip the ones we've already
70 // Create the object that will do the direction-getting.
71 var directions_service = new google.maps.DirectionsService();
72 var requests_submitted = 0;
74 // And ask it to get directions for each producer/consumer pair.
75 for (var i = 0; i < producers.length; i++) {
76 for (var j = 0; j < consumers.length; j++) {
78 var pair_number = (i * consumers.length) + (j+1);
80 if (pair_number <= pairs_to_skip || requests_submitted > 9) {
84 var producer_latlng = new google.maps.LatLng(producers[i].latitude,
85 producers[i].longitude);
87 var consumer_latlng = new google.maps.LatLng(consumers[i].latitude,
88 consumers[i].longitude);
90 var directions_request = {
91 origin: producer_latlng,
92 destination: consumer_latlng,
93 provideTripAlternatives: true,
95 travelMode: google.maps.DirectionsTravelMode.DRIVING
98 /* The function routing_callback(...) gets applied to the result
99 * of route(). Ideally, that result will be a DirectionsResult
100 * object containing directions. This executes
102 directions_service.route(directions_request, routing_callback);
104 requests_submitted += 1;
108 if (pairs_to_skip < total_requests) {
109 // Next time around, skip whatever requests we submitted this
111 pairs_to_skip += requests_submitted;
113 /* 5 seconds is the smallest timeout that will work here. */
114 window.setTimeout("get_directions(" + pairs_to_skip + ");", 5000);
121 function onload_handler(e) {
122 // Avoid problems with the implicit event argument to onload
128 window.onload = onload_handler;