X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=www%2Fmaps%2Fmaps%2Ftemplates%2Fjavascript%2Frouting.mako;h=b3b1c919258830d7005268e231090175f121f3cd;hb=38c6c7b9adbc238d3e6d3a916481f8f83d77d263;hp=94c280e07b6d1036fbdbed4736af50f5c7cf6551;hpb=c0a23c3492a4aa7eefa21c8c5f2a4037000a9493;p=dead%2Fcensus-tools.git diff --git a/www/maps/maps/templates/javascript/routing.mako b/www/maps/maps/templates/javascript/routing.mako index 94c280e..b3b1c91 100644 --- a/www/maps/maps/templates/javascript/routing.mako +++ b/www/maps/maps/templates/javascript/routing.mako @@ -1,25 +1,8 @@ -function post_data(url, data) { - /* Create a new form, and add it to the DOM. Then, create a hidden - input control as a child of the form which contains the data that - we'd like to post. Finally, add the form to the document's body, - and submit it. */ - var new_form = document.createElement("form"); - new_form.method = "post"; - new_form.action = url; - var new_input = document.createElement("input"); - new_input.setAttribute("id", "data"); - new_input.setAttribute("name", "data"); - new_input.setAttribute("type", "hidden"); - new_input.setAttribute("value", data); - new_form.appendChild(new_input); - document.body.appendChild(new_form); - new_form.submit(); -} - - var producers = new Array(); var consumers = new Array(); +var postback_url = "${c.postback_url}"; + /* The URL encoding changes all of our double quotes to ", so we * have to convert them back before we do anything with the JSON * data. */ @@ -50,12 +33,12 @@ function routing_callback(result, status) { if (status == google.maps.DirectionsStatus.OK) { json_string += JSON.stringify(result); - for (var trip_idx = 0; trip_idx < result.trips.length; trip_idx++) { - num_routes = result.trips[trip_idx].routes.length; - if (num_routes > 1) { - alert("Unexpected number of routes (" + - result.trips.routes.length.toString() + - ") on trip number " + trip_idx.toString() + '.'); + for (var route_idx = 0; route_idx < result.routes.length; route_idx++) { + var num_legs = result.routes[route_idx].legs.length; + if (num_legs > 1) { + alert("Unexpected number of legs (" + + result.routes.legs.length.toString() + + ") on route number " + route_idx.toString() + '.'); } } } @@ -68,7 +51,8 @@ function routing_callback(result, status) { if (completed_requests == total_requests) { // Close the array, and do something with the result. json_string += ']'; - post_data('/directions/json_to_kml', json_string) + var F = new Form(); + F.post_data(postback_url, json_string); } else { // Continue the array. @@ -99,17 +83,32 @@ function get_directions(pairs_to_skip) { continue; } - var producer_latlng = new google.maps.LatLng(producers[i].latitude, - producers[i].longitude); - - var consumer_latlng = new google.maps.LatLng(consumers[i].latitude, - consumers[i].longitude); + /* We'll pass these to the DirectionsRequest object. They aren't + * necessarily LatLngs, since if either the latitude or + * longitude is missing, we'll fall back to using the name. */ + var producer_latlng; + var consumer_latlng; + if (producers[i].latitude && producers[i].longitude) { + producer_latlng = new google.maps.LatLng(producers[i].latitude, + producers[i].longitude); + } + else { + producer_latlng = producers[i].name; + } + + if (consumers[j].latitude && consumers[j].longitude) { + consumer_latlng = new google.maps.LatLng(consumers[j].latitude, + consumers[j].longitude); + } + else { + consumer_latlng = consumers[j].name; + } + var directions_request = { origin: producer_latlng, destination: consumer_latlng, - provideTripAlternatives: true, - + provideRouteAlternatives: true, travelMode: google.maps.DirectionsTravelMode.DRIVING }