]> gitweb.michael.orlitzky.com - dead/census-tools.git/blob - www/maps/maps/templates/javascript/routing.mako
Added the "maps" Pylons project.
[dead/census-tools.git] / www / maps / maps / templates / javascript / routing.mako
1 function post_directions_result(url, directions_result) {
2 /* Create a new form, and add it to the DOM. Then, create a hidden
3 input control as a child of the form which contains the data that
4 we'd like to post. Finally, add the form to the document's body,
5 and submit it. */
6 var new_form = document.createElement("form");
7 new_form.method = "post";
8 new_form.action = url;
9 var new_input = document.createElement("input");
10 new_input.setAttribute("id", "directions_result");
11 new_input.setAttribute("name", "directions_result");
12 new_input.setAttribute("type", "hidden");
13 new_input.setAttribute("value", directions_result);
14 new_form.appendChild(new_input);
15 document.body.appendChild(new_form);
16 new_form.submit();
17 }
18
19
20 var producers = new Array();
21 var consumers = new Array();
22
23 /* The URL encoding changes all of our double quotes to ", so we
24 * have to convert them back before we do anything with the JSON
25 * data. */
26 var locations_json = "${c.json}";
27 locations_json = locations_json.replace(/"/g, '"');
28 var locations = JSON.parse(locations_json);
29
30 for (var location_idx = 0; location_idx < locations.length; location_idx++) {
31 if (locations[location_idx].type == 'producer') {
32 producers.push(locations[location_idx]);
33 }
34 else {
35 consumers.push(locations[location_idx]);
36 }
37 }
38
39
40 // Start another JSON array, this time containing our route data.
41 var json_string = '[';
42 var completed_requests = 0;
43 var total_requests = (producers.length * consumers.length);
44
45 function routing_callback(result, status) {
46 /* This is the callback that gets applied to each
47 * DirectionsResult. It occurs asynchronously, which is why we keep
48 * track of the total/completed requests. Only the last call to
49 * complete should actually do something with the final result. */
50 if (status == google.maps.DirectionsStatus.OK) {
51 json_string += JSON.stringify(result);
52
53 for (var trip_idx = 0; trip_idx < result.trips.length; trip_idx++) {
54 num_routes = result.trips[trip_idx].routes.length;
55 if (num_routes > 1) {
56 alert("Unexpected number of routes (" +
57 result.trips.routes.length.toString() +
58 ") on trip number " + trip_idx.toString() + '.');
59 }
60 }
61 }
62 else {
63 alert("Error, status: " + status);
64 }
65
66 completed_requests += 1;
67
68 if (completed_requests == total_requests) {
69 // Close the array, and do something with the result.
70 json_string += ']';
71 post_directions_result('/directions/json_to_kml', json_string)
72 }
73 else {
74 // Continue the array.
75 json_string += ',';
76 }
77
78 }
79
80
81
82 function get_directions(pairs_to_skip) {
83 /* We can only get directions for 10 producer/consumer pairs at a
84 * time. We pass the pairs_to_skip variable back to this function
85 * via setTimeout so that we can skip the ones we've already
86 * submitted. */
87
88 // Create the object that will do the direction-getting.
89 var directions_service = new google.maps.DirectionsService();
90 var requests_submitted = 0;
91
92 // And ask it to get directions for each producer/consumer pair.
93 for (var i = 0; i < producers.length; i++) {
94 for (var j = 0; j < consumers.length; j++) {
95
96 var pair_number = (i * consumers.length) + (j+1);
97
98 if (pair_number <= pairs_to_skip || requests_submitted > 9) {
99 continue;
100 }
101
102 var producer_latlng = new google.maps.LatLng(producers[i].latitude,
103 producers[i].longitude);
104
105 var consumer_latlng = new google.maps.LatLng(consumers[i].latitude,
106 consumers[i].longitude);
107
108 var directions_request = {
109 origin: producer_latlng,
110 destination: consumer_latlng,
111 provideTripAlternatives: true,
112
113 travelMode: google.maps.DirectionsTravelMode.DRIVING
114 }
115
116 /* The function routing_callback(...) gets applied to the result
117 * of route(). Ideally, that result will be a DirectionsResult
118 * object containing directions. This executes
119 * asynchronously. */
120 directions_service.route(directions_request, routing_callback);
121
122 requests_submitted += 1;
123 }
124 }
125
126 if (pairs_to_skip < total_requests) {
127 // Next time around, skip whatever requests we submitted this
128 // time.
129 pairs_to_skip += requests_submitted;
130
131 /* 5 seconds is the smallest timeout that will work here. */
132 window.setTimeout("get_directions(" + pairs_to_skip + ");", 5000);
133 }
134
135 return;
136 }
137
138
139 function onload_handler(e) {
140 // Avoid problems with the implicit event argument to onload
141 // handlers.
142 get_directions(0);
143 }
144
145
146 window.onload = onload_handler;