]> gitweb.michael.orlitzky.com - dead/census-tools.git/blob - www/maps/maps/templates/javascript/routing.mako
Created a MapUtils object to contain the post_data function.
[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 mu = new MapUtils();
53 mu.post_data('/directions/json_to_kml', json_string);
54 }
55 else {
56 // Continue the array.
57 json_string += ',';
58 }
59
60 }
61
62
63
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
68 * submitted. */
69
70 // Create the object that will do the direction-getting.
71 var directions_service = new google.maps.DirectionsService();
72 var requests_submitted = 0;
73
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++) {
77
78 var pair_number = (i * consumers.length) + (j+1);
79
80 if (pair_number <= pairs_to_skip || requests_submitted > 9) {
81 continue;
82 }
83
84 var producer_latlng = new google.maps.LatLng(producers[i].latitude,
85 producers[i].longitude);
86
87 var consumer_latlng = new google.maps.LatLng(consumers[i].latitude,
88 consumers[i].longitude);
89
90 var directions_request = {
91 origin: producer_latlng,
92 destination: consumer_latlng,
93 provideTripAlternatives: true,
94
95 travelMode: google.maps.DirectionsTravelMode.DRIVING
96 }
97
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
101 * asynchronously. */
102 directions_service.route(directions_request, routing_callback);
103
104 requests_submitted += 1;
105 }
106 }
107
108 if (pairs_to_skip < total_requests) {
109 // Next time around, skip whatever requests we submitted this
110 // time.
111 pairs_to_skip += requests_submitted;
112
113 /* 5 seconds is the smallest timeout that will work here. */
114 window.setTimeout("get_directions(" + pairs_to_skip + ");", 5000);
115 }
116
117 return;
118 }
119
120
121 function onload_handler(e) {
122 // Avoid problems with the implicit event argument to onload
123 // handlers.
124 get_directions(0);
125 }
126
127
128 window.onload = onload_handler;