var map = null; var geocoder = new GClientGeocoder(); var base_icon = new GIcon(G_DEFAULT_ICON); base_icon.shadow = 'http://www.google.com/mapfiles/shadow50.png'; base_icon.iconSize = new GSize(20, 34); base_icon.shadowSize = new GSize(37, 34); base_icon.iconAnchor = new GPoint(9, 34); base_icon.infoWindowAnchor = new GPoint(9, 2); var init_point = new GLatLng(44.5, -30.0); var icon1 = new GIcon(base_icon); icon1.image = 'http://www.google.com/mapfiles/markerS.png'; var icon2 = new GIcon(base_icon); icon2.image = 'http://www.google.com/mapfiles/markerD.png'; var marker1 = new GMarker(new GLatLng(48.1391265, 11.5801863), {draggable:true, icon:icon1}); // start point var marker2 = new GMarker(new GLatLng(40.756054, -73.986951), {draggable:true, icon:icon2}); // destination var search_marker = new Object(); var polyline = new GPolyline([marker1.getLatLng(), marker2.getLatLng()], "#ff0000", 10); $(document).ready(function() { $('#start_time_picker').datepicker({ duration:'', showTime:true, constrainInput:true, stepMinutes:1, stepHours:1, altTimeField:'', time24h:true, currentText:'Today', closeText:'Calculate', onClose:function(){ calculate(); } }); $('#search_input').keypress(function(e) { key = e.which ? e.which : e.keyCode; if (key == 13) { get_point($('#search_input').val()); } }); }); function initialize() { if (GBrowserIsCompatible()) { map = new GMap2(document.getElementById('map')); map.setUIToDefault(); map.setCenter(init_point, 2); map.addOverlay(marker1); map.addOverlay(marker2); map.setMapType(G_PHYSICAL_MAP); refresh(); GEvent.addListener(marker1, 'dragend', refresh); GEvent.addListener(marker2, 'dragend', refresh); map.addOverlay(polyline); } } function use_search(point) { if (point == 'startpoint') { marker1.setLatLng(search_marker.getLatLng()); } else if (point == 'destination') { marker2.setLatLng(search_marker.getLatLng()); } map.removeOverlay(search_marker); refresh(); } function refresh() { var point1 = marker1.getLatLng(); var point2 = marker2.getLatLng(); map.removeOverlay(polyline); polyline = new GPolyline([point1, point2], '#ff0000', 10); map.addOverlay(polyline); } function get_point(address) { geocoder.getLatLng(address, function(point) { if (!point) { alert('This location could not be found.') } else { map.setCenter(point); map.removeOverlay(search_marker); search_marker = new GMarker(point); map.addOverlay(search_marker); search_marker.openInfoWindowHtml('Use this point as:
start point (S)\
destination (D)
'); } }); } function show_timepicker() { $('#start_time_picker').datepicker('show'); } function calculate() { var point1 = marker1.getLatLng(); var point2 = marker2.getLatLng(); var start_time = $('#start_time_picker').datepicker('getDate').getTime() / 1000; // JavaScript in ms start_time += $('body').find('.fragHours').text()*60*60; start_time += $('body').find('.fragMinutes').text()*60; var get_data = {lat1:point1.lat(), lng1:point1.lng(), lat2:point2.lat(), lng2:point2.lng(), start_time:start_time}; $.ajax({ url:'/calculate', data:get_data, dataType:'json', success:function(data, textStatus, XMLHttpRequest) { $('#result').html(format( 'Flight distance: {0}km
\ Ground distance: {1}km
\ Flighttime: {2}h {3}min\ \ \ \ \
Start time:{4}
Arrival time:{5}
(Local times)
', data.flight_distance, data.ground_distance, data.flight_hours, data.flight_minutes, data.start_time, data.arrival_time) ); $('#result').show(); } }); } // str-formatter: format("str{0} {1}", var1, var2) function format(str) { for (i=1; i