]> gitweb.michael.orlitzky.com - dead/census-tools.git/blob - www/maps/maps/templates/javascript/routing.mako
ecab0247e9252e8b1af10409f41ef6e039dd3057
[dead/census-tools.git] / www / maps / maps / templates / javascript / routing.mako
1 var producers = new Array();
2 var consumers = new Array();
3
4 var postback_url = "${c.postback_url}";
5
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
8 * data. */
9 var locations_json = "${c.json}";
10 locations_json = locations_json.replace(/"/g, '"');
11 var locations = JSON.parse(locations_json);
12
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]);
16 }
17 else {
18 consumers.push(locations[location_idx]);
19 }
20 }
21
22
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);
27
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);
35
36 for (var trip_idx = 0; trip_idx < result.trips.length; trip_idx++) {
37 num_routes = result.trips[trip_idx].routes.length;
38 if (num_routes > 1) {
39 alert("Unexpected number of routes (" +
40 result.trips.routes.length.toString() +
41 ") on trip number " + trip_idx.toString() + '.');
42 }
43 }
44 }
45 else {
46 alert("Error, status: " + status);
47 }
48
49 completed_requests += 1;
50
51 if (completed_requests == total_requests) {
52 // Close the array, and do something with the result.
53 json_string += ']';
54 var F = new Form();
55 F.post_data(postback_url, json_string);
56 }
57 else {
58 // Continue the array.
59 json_string += ',';
60 }
61
62 }
63
64
65
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
70 * submitted. */
71
72 // Create the object that will do the direction-getting.
73 var directions_service = new google.maps.DirectionsService();
74 var requests_submitted = 0;
75
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++) {
79
80 var pair_number = (i * consumers.length) + (j+1);
81
82 if (pair_number <= pairs_to_skip || requests_submitted > 9) {
83 continue;
84 }
85
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. */
89 var producer_latlng;
90 var consumer_latlng;
91
92 if (producers[i].latitude && producers[i].longitude) {
93 producer_latlng = new google.maps.LatLng(producers[i].latitude,
94 producers[i].longitude);
95 }
96 else {
97 producer_latlng = producers[i].name;
98 }
99
100 if (consumers[i].latitude && consumers[i].longitude) {
101 consumer_latlng = new google.maps.LatLng(consumers[i].latitude,
102 consumers[i].longitude);
103 }
104 else {
105 consumer_latlng = consumers[i].name;
106 }
107
108 var directions_request = {
109 origin: producer_latlng,
110 destination: consumer_latlng,
111 provideTripAlternatives: true,
112 travelMode: google.maps.DirectionsTravelMode.DRIVING
113 }
114
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
118 * asynchronously. */
119 directions_service.route(directions_request, routing_callback);
120
121 requests_submitted += 1;
122 }
123 }
124
125 if (pairs_to_skip < total_requests) {
126 // Next time around, skip whatever requests we submitted this
127 // time.
128 pairs_to_skip += requests_submitted;
129
130 /* 5 seconds is the smallest timeout that will work here. */
131 window.setTimeout("get_directions(" + pairs_to_skip + ");", 5000);
132 }
133
134 return;
135 }
136
137
138 function onload_handler(e) {
139 // Avoid problems with the implicit event argument to onload
140 // handlers.
141 get_directions(0);
142 }
143
144
145 window.onload = onload_handler;