1 var producers = new Array();
2 var consumers = new Array();
4 var postback_url = "${c.postback_url}";
6 /* The URL encoding changes all of our double quotes to ", so we
7 * have to convert them back before we do anything with the JSON
9 var locations_json = "${c.json}";
10 locations_json = locations_json.replace(/"/g, '"');
11 var locations = JSON.parse(locations_json);
13 for (var location_idx = 0; location_idx < locations.length; location_idx++) {
14 if (locations[location_idx].type == 'producer') {
15 producers.push(locations[location_idx]);
18 consumers.push(locations[location_idx]);
23 // Start another JSON array, this time containing our route data.
24 var json_string = '[';
25 var completed_requests = 0;
26 var total_requests = (producers.length * consumers.length);
28 function routing_callback(result, status) {
29 /* This is the callback that gets applied to each
30 * DirectionsResult. It occurs asynchronously, which is why we keep
31 * track of the total/completed requests. Only the last call to
32 * complete should actually do something with the final result. */
33 if (status == google.maps.DirectionsStatus.OK) {
34 json_string += JSON.stringify(result);
36 for (var trip_idx = 0; trip_idx < result.trips.length; trip_idx++) {
37 num_routes = result.trips[trip_idx].routes.length;
39 alert("Unexpected number of routes (" +
40 result.trips.routes.length.toString() +
41 ") on trip number " + trip_idx.toString() + '.');
46 alert("Error, status: " + status);
49 completed_requests += 1;
51 if (completed_requests == total_requests) {
52 // Close the array, and do something with the result.
55 F.post_data(postback_url, json_string);
58 // Continue the array.
66 function get_directions(pairs_to_skip) {
67 /* We can only get directions for 10 producer/consumer pairs at a
68 * time. We pass the pairs_to_skip variable back to this function
69 * via setTimeout so that we can skip the ones we've already
72 // Create the object that will do the direction-getting.
73 var directions_service = new google.maps.DirectionsService();
74 var requests_submitted = 0;
76 // And ask it to get directions for each producer/consumer pair.
77 for (var i = 0; i < producers.length; i++) {
78 for (var j = 0; j < consumers.length; j++) {
80 var pair_number = (i * consumers.length) + (j+1);
82 if (pair_number <= pairs_to_skip || requests_submitted > 9) {
86 /* We'll pass these to the DirectionsRequest object. They aren't
87 * necessarily LatLngs, since if either the latitude or
88 * longitude is missing, we'll fall back to using the name. */
92 if (producers[i].latitude && producers[i].longitude) {
93 producer_latlng = new google.maps.LatLng(producers[i].latitude,
94 producers[i].longitude);
97 producer_latlng = producers[i].name;
100 if (consumers[i].latitude && consumers[i].longitude) {
101 consumer_latlng = new google.maps.LatLng(consumers[i].latitude,
102 consumers[i].longitude);
105 consumer_latlng = consumers[i].name;
108 var directions_request = {
109 origin: producer_latlng,
110 destination: consumer_latlng,
111 provideTripAlternatives: true,
112 travelMode: google.maps.DirectionsTravelMode.DRIVING
115 /* The function routing_callback(...) gets applied to the result
116 * of route(). Ideally, that result will be a DirectionsResult
117 * object containing directions. This executes
119 directions_service.route(directions_request, routing_callback);
121 requests_submitted += 1;
125 if (pairs_to_skip < total_requests) {
126 // Next time around, skip whatever requests we submitted this
128 pairs_to_skip += requests_submitted;
130 /* 5 seconds is the smallest timeout that will work here. */
131 window.setTimeout("get_directions(" + pairs_to_skip + ");", 5000);
138 function onload_handler(e) {
139 // Avoid problems with the implicit event argument to onload
145 window.onload = onload_handler;