]> gitweb.michael.orlitzky.com - dead/census-tools.git/blob - www/maps/maps/templates/javascript/routing.mako
Moved the post_data() function in to its own static Javascript file.
[dead/census-tools.git] / www / maps / maps / templates / javascript / routing.mako
1 var producers = new Array();
2 var consumers = new Array();
3
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
6 * data. */
7 var locations_json = "${c.json}";
8 locations_json = locations_json.replace(/"/g, '"');
9 var locations = JSON.parse(locations_json);
10
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]);
14 }
15 else {
16 consumers.push(locations[location_idx]);
17 }
18 }
19
20
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);
25
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);
33
34 for (var trip_idx = 0; trip_idx < result.trips.length; trip_idx++) {
35 num_routes = result.trips[trip_idx].routes.length;
36 if (num_routes > 1) {
37 alert("Unexpected number of routes (" +
38 result.trips.routes.length.toString() +
39 ") on trip number " + trip_idx.toString() + '.');
40 }
41 }
42 }
43 else {
44 alert("Error, status: " + status);
45 }
46
47 completed_requests += 1;
48
49 if (completed_requests == total_requests) {
50 // Close the array, and do something with the result.
51 json_string += ']';
52 post_data('/directions/json_to_kml', json_string)
53 }
54 else {
55 // Continue the array.
56 json_string += ',';
57 }
58
59 }
60
61
62
63 function get_directions(pairs_to_skip) {
64 /* We can only get directions for 10 producer/consumer pairs at a
65 * time. We pass the pairs_to_skip variable back to this function
66 * via setTimeout so that we can skip the ones we've already
67 * submitted. */
68
69 // Create the object that will do the direction-getting.
70 var directions_service = new google.maps.DirectionsService();
71 var requests_submitted = 0;
72
73 // And ask it to get directions for each producer/consumer pair.
74 for (var i = 0; i < producers.length; i++) {
75 for (var j = 0; j < consumers.length; j++) {
76
77 var pair_number = (i * consumers.length) + (j+1);
78
79 if (pair_number <= pairs_to_skip || requests_submitted > 9) {
80 continue;
81 }
82
83 var producer_latlng = new google.maps.LatLng(producers[i].latitude,
84 producers[i].longitude);
85
86 var consumer_latlng = new google.maps.LatLng(consumers[i].latitude,
87 consumers[i].longitude);
88
89 var directions_request = {
90 origin: producer_latlng,
91 destination: consumer_latlng,
92 provideTripAlternatives: true,
93
94 travelMode: google.maps.DirectionsTravelMode.DRIVING
95 }
96
97 /* The function routing_callback(...) gets applied to the result
98 * of route(). Ideally, that result will be a DirectionsResult
99 * object containing directions. This executes
100 * asynchronously. */
101 directions_service.route(directions_request, routing_callback);
102
103 requests_submitted += 1;
104 }
105 }
106
107 if (pairs_to_skip < total_requests) {
108 // Next time around, skip whatever requests we submitted this
109 // time.
110 pairs_to_skip += requests_submitted;
111
112 /* 5 seconds is the smallest timeout that will work here. */
113 window.setTimeout("get_directions(" + pairs_to_skip + ");", 5000);
114 }
115
116 return;
117 }
118
119
120 function onload_handler(e) {
121 // Avoid problems with the implicit event argument to onload
122 // handlers.
123 get_directions(0);
124 }
125
126
127 window.onload = onload_handler;