Moved all Google Maps functions to their own js include file, leaving only functions explicitly related to the predictor itself in pred.js

pull/73/head
jonsowman 2011-01-14 00:32:40 +00:00
rodzic f5584e4e7a
commit 73da93853f
3 zmienionych plików z 351 dodań i 292 usunięć

Wyświetl plik

@ -39,9 +39,10 @@ google.load("jqueryui", "1.8.1");
<script src="js/jquery.tipsy.js" type="text/javascript"></script>
<script src="js/date.jsport.js" type="text/javascript"></script>
<script src="js/config.js" type="text/javascript"></script>
<script src="js/pred.js" type="text/javascript"></script>
<script src="js/pred-ui.js" type="text/javascript"></script>
<script src="js/pred-cookie.js" type="text/javascript"></script>
<script src="js/pred-map.js" type="text/javascript"></script>
<script src="js/pred.js" type="text/javascript"></script>
<script src="js/calc.js" type="text/javascript"></script>
</head>
<body>

Wyświetl plik

@ -0,0 +1,215 @@
/*
* CUSF Landing Prediction Version 2
* Jon Sowman 2010
* jon@hexoc.com
* http://www.hexoc.com
*
* http://github.com/jonsowman/cusf-standalone-predictor
*
* This file contains all of the prediction javascript functions
* that are explicitly related to Google Map manipulation
*
*/
// Initialise the map canvas with (lat, long, zoom)
function initMap(centre_lat, centre_lon, zoom_level) {
// Make the map and set center
var latlng = new google.maps.LatLng(centre_lat, centre_lon);
var myOptions = {
zoom: zoom_level,
scaleControl: true,
scaleControlOptions: { position: google.maps.ControlPosition.BOTTOM_LEFT } ,
mapTypeId: google.maps.MapTypeId.TERRAIN,
center: latlng
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
// Enable or disable user control of the map canvas, including scrolling,
// zooming and clicking
function enableMap(map, state) {
if ( state != false && state != true) {
appendDebug("Unrecognised map state");
} else if (state == false) {
map.draggable = false;
map.disableDoubleClickZoom = true;
map.scrollwheel = false;
map.navigationControl = false;
} else if (state == true ) {
map.draggable = true;
map.disableDoubleClickZoom = false;
map.scrollwheel = false;
map.navigationControl = true;
}
}
// This should be called on a "mousemove" event handler on the map canvas
// and will update scenario information display
function showMousePos(GLatLng) {
var curr_lat = GLatLng.lat().toFixed(4);
var curr_lon = GLatLng.lng().toFixed(4);
$("#cursor_lat").html(curr_lat);
$("#cursor_lon").html(curr_lon);
// if we have a prediction displayed
// show range from launch and land:
if ( current_uuid != 0 && map_items['launch_marker'] != null ) {
var launch_pt = map_items['launch_marker'].position;
var land_pt = map_items['land_marker'].position;
var range_launch = distHaversine(launch_pt, GLatLng, 1);
var range_land = distHaversine(land_pt, GLatLng, 1);
$("#cursor_pred_launchrange").html(range_launch);
$("#cursor_pred_landrange").html(range_land);
}
}
// Takes an array of points and the name of the gmap object and plots
// the polygon onto the map
function drawPolygon(points, gmap_object) {
var newPoly = new google.maps.Polyline({
path: points,
strokeColor: "#FF0000",
strokeOpacity: 0.4,
//fillColor: "#FFFFFF",
//fillOpacity: 0,
strokeWeight: 2
});
map_items['delta_square'] = newPoly;
newPoly.setMap(gmap_object);
}
// Read the latitude and longitude currently in the launch card and plot
// a marker there with hover information
function plotClick() {
// Clear the old marker
clearMapItems();
// Get the new values from the form
click_lat = parseFloat($("#lat").val());
click_lon = parseFloat($("#lon").val());
// Make sure the data is valid before we try and do anything with it
if ( isNaN(click_lat) || isNaN(click_lon) ) return;
var click_pt = new google.maps.LatLng(click_lat, click_lon);
clickMarker = new google.maps.Marker({
position: click_pt,
map: map,
icon: 'images/target-1-sm.png',
title: 'Currently selected launch location (' + click_lat + ', '
+ click_lon+')'
});
map_items['clickMarker'] = clickMarker;
// Redraw the delta square
drawDeltaSquare(map);
map.panTo(click_pt);
map.setZoom(8);
}
// Uses the currently selected lat, lon and delta values in the launch
// card to draw a square of the GFS data to be downloaded for the prediction
function drawDeltaSquare(map) {
// Clear the old delta square if it exists
if ( map_items['delta_square'] ) map_items['delta_square'].setMap(null);
// Get the values from the form
var lat = parseFloat($("#lat").val());
var lon = parseFloat($("#lon").val());
var dlat = parseFloat($("#delta_lat").val());
var dlon = parseFloat($("#delta_lon").val());
// Construct a rectange of points
var points = [
new google.maps.LatLng(lat+dlat, lon+dlon),
new google.maps.LatLng(lat-dlat, lon+dlon),
new google.maps.LatLng(lat-dlat, lon-dlon),
new google.maps.LatLng(lat+dlat, lon-dlon),
new google.maps.LatLng(lat+dlat, lon+dlon)
]
// Draw this polygon onto the map canvas
drawPolygon(points, map);
}
// Given a GLatLng object, write the latitude and longitude to the launch card
function setFormLatLon(GLatLng) {
appendDebug("Trying to set the form lat long");
$("#lat").val(GLatLng.lat().toFixed(4));
$("#lon").val(GLatLng.lng().toFixed(4));
// Remove the event handler so another click doesn't register
setLatLonByClick(false);
// Change the dropdown to read "other"
SetSiteOther();
// Plot the new marker for launch location
appendDebug("Plotting the new launch location marker");
plotClick();
}
// Enable or disable an event handler which, when a mouse click is detected
// on the map canvas, will write the coordinates of the clicked place to the
// launch card
function setLatLonByClick(state) {
if ( state == true ) {
// Check this listener doesn't already exist
if (!clickListener) {
appendDebug("Enabling the set with click listener");
clickListener = google.maps.event.addListener(map, 'click', function(event) {
appendDebug("Got a click from user, setting values into form");
$("#error_window").fadeOut();
setFormLatLon(event.latLng);
});
}
// Tell the user what to do next
throwError("Now click your desired launch location on the map");
} else if ( state == false ) {
appendDebug("Removing the set with click listener");
google.maps.event.removeListener(clickListener);
clickListener = null;
} else {
appendDebug("Unrecognised state for setLatLonByClick");
}
}
// An associative array exists globally containing all objects we have placed
// onto the map canvas - this function clears all of them
function clearMapItems() {
$("#cursor_pred").hide();
if( getAssocSize(map_items) > 0 ) {
appendDebug("Clearing previous map trace");
for( i in map_items ) {
map_items[i].setMap(null);
}
}
map_items = [];
}
// The Haversine formula to calculate the distance across the surface between
// two points on the Earth
distHaversine = function(p1, p2, precision) {
var R = 6371; // earth's mean radius in km
var dLat = rad(p2.lat() - p1.lat());
var dLong = rad(p2.lng() - p1.lng());
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) * Math.sin(dLong/2) * Math.sin(dLong/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
if ( precision == null ) {
return d.toFixed(3);
} else {
return d.toFixed(precision);
}
}
// Given a latitude, longitude, and a field to write the result to,
// find the name of the place using Google "reverse Geocode" API feature
function rvGeocode(lat, lon, fillField) {
var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(parseFloat(lat), parseFloat(lon));
var coded = "Unnamed";
geocoder.geocode({'latLng': latlng}, function(results, status) {
if ( status == google.maps.GeocoderStatus.OK ) {
// Successfully got rv-geocode information
appendDebug("Got a good response from the geocode server");
coded = results[1].address_components[1].short_name;
} else {
appendDebug("The rv-geocode failed: " + status);
}
// Now write the value to the field
$("#"+fillField+"").val(coded);
});
}

Wyświetl plik

@ -8,9 +8,10 @@
*
*/
// This function runs when the document object model is fully populated
// and the page is loaded
$(document).ready(function() {
// Initialise the map canvas
// Initialise the map canvas with parameters (lat, long, zoom-level)
initMap(52, 0, 8);
// Populate the launch site list from sites.json
@ -116,24 +117,6 @@ function addHashLink(link) {
window.location = ln;
}
function showMousePos(GLatLng) {
var curr_lat = GLatLng.lat().toFixed(4);
var curr_lon = GLatLng.lng().toFixed(4);
$("#cursor_lat").html(curr_lat);
$("#cursor_lon").html(curr_lon);
// if we have a prediction displayed
// show range from launch and land:
if ( current_uuid != 0 && map_items['launch_marker'] != null ) {
var launch_pt = map_items['launch_marker'].position;
var land_pt = map_items['land_marker'].position;
var range_launch = distHaversine(launch_pt, GLatLng, 1);
var range_land = distHaversine(land_pt, GLatLng, 1);
$("#cursor_pred_launchrange").html(range_launch);
$("#cursor_pred_landrange").html(range_land);
}
}
function populateLaunchSite() {
$("#site > option").remove();
$.getJSON("sites.json", function(sites) {
@ -148,16 +131,23 @@ function populateLaunchSite() {
function changeLaunchSite() {
var selectedName = $("#site").val();
$.getJSON("sites.json", function(sites) {
$.each(sites, function(sitename, site) {
if ( selectedName == sitename ) {
$("#lat").val(site.latitude);
$("#lon").val(site.longitude);
$("#initial_alt").val(site.altitude);
}
if ( selectedName == "Other" ) {
appendDebug("User requested locally saved launch sites");
if ( constructCookieLocationsTable("cusf_predictor") ) {
$("#location_save_local").fadeIn();
}
} else {
$.getJSON("sites.json", function(sites) {
$.each(sites, function(sitename, site) {
if ( selectedName == sitename ) {
$("#lat").val(site.latitude);
$("#lon").val(site.longitude);
$("#initial_alt").val(site.altitude);
}
});
plotClick();
});
plotClick();
});
}
}
function writePredictionInfo(current_uuid, run_time, gfs_timestamp) {
@ -179,13 +169,15 @@ function handlePred(pred_uuid) {
// disable user control of the map canvas
$("#map_canvas").fadeTo(1000, 0.2);
// ajax to poll for progress
ajaxEventHandle = setInterval("getJSONProgress('"+pred_uuid+"')", stdPeriod);
ajaxEventHandle = setInterval("getJSONProgress('"
+ pred_uuid + "')", stdPeriod);
}
function getCSV(pred_uuid) {
$.get("ajax.php", { "action":"getCSV", "uuid":pred_uuid }, function(data) {
if(data != null) {
appendDebug("Got JSON response from server for flight path, parsing...");
appendDebug("Got JSON response from server for flight path,"
+ " parsing...");
if (parseCSV(data) ) {
appendDebug("Parsing function returned successfully.");
appendDebug("Done, AJAX functions quitting.");
@ -217,13 +209,16 @@ function getJSONProgress(pred_uuid) {
ajaxTimeout = newTimeout;
} else if ( ajaxTimeout != hlTimeout ) {
// otherwise, increase poll delay and timeout
appendDebug("Reached maximum ajaxTimeout value of " + maxAjaxTimeout);
appendDebug("Reached maximum ajaxTimeout value of "
+ maxAjaxTimeout);
clearInterval(ajaxEventHandle);
appendDebug("Switching to high latency mode");
appendDebug("Setting polling interval to "+hlPeriod+"ms");
appendDebug("Setting progress JSON timeout to "+hlTimeout+"ms");
appendDebug("Setting progress JSON timeout to "
+ hlTimeout + "ms");
ajaxTimeout = hlTimeout;
ajaxEventHandle = setInterval("getJSONProgress('"+pred_uuid+"')", hlPeriod);
ajaxEventHandle = setInterval("getJSONProgress('"
+ pred_uuid + "')", hlPeriod);
}
}
},
@ -234,7 +229,8 @@ function getJSONProgress(pred_uuid) {
function processProgress(progress) {
if ( progress['error'] ) {
clearInterval(ajaxEventHandle);
appendDebug("There was an error in running the prediction: "+progress['error']);
appendDebug("There was an error in running the prediction: "
+ progress['error']);
} else {
// get the progress of the wind data
if ( progress['gfs_complete'] == true ) {
@ -248,9 +244,12 @@ function processProgress(progress) {
clearInterval(ajaxEventHandle);
// parse the data
getCSV(current_uuid);
appendDebug("Server gave a prediction run timestamp of "+progress['run_time']);
appendDebug("Server said it used the " + progress['gfs_timestamp'] + " GFS model");
writePredictionInfo(current_uuid, progress['run_time'], progress['gfs_timestamp']);
appendDebug("Server gave a prediction run timestamp of "
+ progress['run_time']);
appendDebug("Server said it used the "
+ progress['gfs_timestamp'] + " GFS model");
writePredictionInfo(current_uuid, progress['run_time'],
progress['gfs_timestamp']);
addHashLink("uuid="+current_uuid);
} else if ( progress['pred_running'] != true ) {
$("#prediction_status").html("Waiting for predictor to run...");
@ -264,7 +263,8 @@ function processProgress(progress) {
$("#prediction_progress").progressbar("option", "value",
progress['gfs_percent']);
$("#prediction_percent").html(progress['gfs_percent'] +
"% - Estimated time remaining: " + progress['gfs_timeremaining']);
"% - Estimated time remaining: "
+ progress['gfs_timeremaining']);
appendDebug("Server says: downloaded " +
progress['gfs_percent'] + "% of GFS files");
}
@ -273,7 +273,7 @@ function processProgress(progress) {
}
function parseCSV(lines) {
if(lines.length <= 0) {
if( lines.length <= 0 ) {
appendDebug("The server returned an empty CSV file");
return false;
}
@ -292,39 +292,45 @@ function parseCSV(lines) {
var burst_time;
var launch_time;
var land_time;
$.each(lines, function(idx, line) {
entry = line.split(',');
if(entry.length >= 4) { // check valid entry length
var point = new google.maps.LatLng( parseFloat(entry[1]), parseFloat(entry[2]) );
if ( idx == 0 ) { // get the launch lat/long for marker
launch_lat = entry[1];
launch_lon = entry[2];
launch_time = entry[0];
launch_pt = point;
}
// set on every iteration, last valid entry
// gives landing position
land_lat = entry[1];
land_lon = entry[2];
land_time = entry[0];
land_pt = point;
if(parseFloat(entry[3]) > max_height) {
max_height = parseFloat(entry[3]);
burst_pt = point;
burst_lat = entry[1];
burst_lon = entry[2];
burst_time = entry[0];
}
path.push(point);
$.each(lines, function(idx, line) {
entry = line.split(',');
// Check for a valid entry length
if(entry.length >= 4) {
var point = new google.maps.LatLng( parseFloat(entry[1]),
parseFloat(entry[2]) );
// Get launch lat/lon
if ( idx == 0 ) {
launch_lat = entry[1];
launch_lon = entry[2];
launch_time = entry[0];
launch_pt = point;
}
});
// Set on every iteration such that last valid entry gives the
// landing position
land_lat = entry[1];
land_lon = entry[2];
land_time = entry[0];
land_pt = point;
// Find the burst lat/lon/alt
if( parseFloat(entry[3]) > max_height ) {
max_height = parseFloat(entry[3]);
burst_pt = point;
burst_lat = entry[1];
burst_lon = entry[2];
burst_time = entry[0];
}
// Push the point onto the polyline path
path.push(point);
}
});
appendDebug("Flight data parsed, creating map plot...");
clearMapItems();
// calculate range and time of flight
// Calculate range and time of flight
var range = distHaversine(launch_pt, land_pt, 1);
var flighttime = land_time - launch_time;
var f_hours = Math.floor((flighttime % 86400) / 3600);
@ -335,7 +341,7 @@ function parseCSV(lines) {
$("#cursor_pred_time").html(flighttime);
$("#cursor_pred").show();
// make some nice icons
// Make some nice icons
var launch_icon = new google.maps.MarkerImage(launch_img,
new google.maps.Size(16,16),
new google.maps.Point(0, 0),
@ -352,14 +358,16 @@ function parseCSV(lines) {
position: launch_pt,
map: map,
icon: launch_icon,
title: 'Balloon launch ('+launch_lat+', '+launch_lon+') at ' + POSIXtoHM(launch_time) + "UTC"
title: 'Balloon launch ('+launch_lat+', '+launch_lon+') at '
+ POSIXtoHM(launch_time) + "UTC"
});
var land_marker = new google.maps.Marker({
position: land_pt,
map:map,
icon: land_icon,
title: 'Predicted Landing ('+land_lat+', '+land_lon+') at ' + POSIXtoHM(land_time) + "UTC"
title: 'Predicted Landing ('+land_lat+', '+land_lon+') at '
+ POSIXtoHM(land_time) + "UTC"
});
var path_polyline = new google.maps.Polyline({
@ -374,145 +382,27 @@ function parseCSV(lines) {
position: burst_pt,
map: map,
icon: burst_img,
title: 'Balloon burst ('+burst_lat+', '+burst_lon+' at altitude ' + max_height + 'm) at ' + POSIXtoHM(burst_time) + "UTC"
title: 'Balloon burst (' + burst_lat + ', ' + burst_lon
+ ' at altitude ' + max_height + 'm) at '
+ POSIXtoHM(burst_time) + "UTC"
});
// now add the launch/land markers to map
// we might need these later, so push them
// associatively
// Add the launch/land markers to map
// We might need access to these later, so push them associatively
map_items['launch_marker'] = launch_marker;
map_items['land_marker'] = land_marker;
map_items['pop_marker'] = pop_marker;
map_items['path_polyline'] = path_polyline;
// we wiped off the old delta square,
// and it may have changed anyway, so re-plot
// We wiped off the old delta square,
// And it may have changed anyway, so re-plot
drawDeltaSquare(map);
// pan to the new position
// Pan to the new position
map.panTo(launch_pt);
map.setZoom(8);
return true;
}
function drawPolygon(points, gmap_object) {
var newPoly = new google.maps.Polyline({
path: points,
strokeColor: "#FF0000",
strokeOpacity: 0.4,
//fillColor: "#FFFFFF",
//fillOpacity: 0,
strokeWeight: 2
});
map_items['delta_square'] = newPoly;
newPoly.setMap(gmap_object);
}
function plotClick() {
// clear the old marker
clearMapItems();
// get the new values from the form
click_lat = parseFloat($("#lat").val());
click_lon = parseFloat($("#lon").val());
// Make sure the data is valid before we try and do anything with it
if ( isNaN(click_lat) || isNaN(click_lon) ) return;
var click_pt = new google.maps.LatLng(click_lat, click_lon);
clickMarker = new google.maps.Marker({
position: click_pt,
map: map,
icon: 'images/target-1-sm.png',
title: 'Currently selected launch location ('+click_lat+', '+click_lon+')'
});
map_items['clickMarker'] = clickMarker;
// redraw the delta square
drawDeltaSquare(map);
map.panTo(click_pt);
map.setZoom(8);
}
function drawDeltaSquare(map) {
// clear any old squares
if ( map_items['delta_square'] ) map_items['delta_square'].setMap(null);
// get the values from the form
var lat = parseFloat($("#lat").val());
var lon = parseFloat($("#lon").val());
var dlat = parseFloat($("#delta_lat").val());
var dlon = parseFloat($("#delta_lon").val());
// make a rectange of points
var points = [
new google.maps.LatLng(lat+dlat, lon+dlon),
new google.maps.LatLng(lat-dlat, lon+dlon),
new google.maps.LatLng(lat-dlat, lon-dlon),
new google.maps.LatLng(lat+dlat, lon-dlon),
new google.maps.LatLng(lat+dlat, lon+dlon)
]
// write the poly to the map
drawPolygon(points, map);
}
function setFormLatLon(GLatLng) {
appendDebug("Trying to set the form lat long");
$("#lat").val(GLatLng.lat().toFixed(4));
$("#lon").val(GLatLng.lng().toFixed(4));
// remove the event handler so another click doesn't register
setLatLonByClick(false);
// change the dropdown to read "other"
SetSiteOther();
// plot the new marker for launch location
appendDebug("Plotting the new launch location marker");
plotClick();
}
function setLatLonByClick(state) {
if ( state == true ) {
// check this listener doesn't already exist
if (!clickListener) {
appendDebug("Enabling the set with click listener");
clickListener = google.maps.event.addListener(map, 'click', function(event) {
appendDebug("Got a click from user, setting values into form");
$("#error_window").fadeOut();
setFormLatLon(event.latLng);
});
}
// tell the user what to do next
throwError("Now click your desired launch location on the map");
} else if ( state == false ) {
appendDebug("Removing the set with click listener");
google.maps.event.removeListener(clickListener);
clickListener = null;
} else {
appendDebug("Unrecognised state for setLatLonByClick");
}
}
function enableMap(map, state) {
if ( state != false && state != true) {
appendDebug("Unrecognised map state");
} else if (state == false) {
map.draggable = false;
map.disableDoubleClickZoom = true;
map.scrollwheel = false;
map.navigationControl = false;
} else if (state == true ) {
map.draggable = true;
map.disableDoubleClickZoom = false;
map.scrollwheel = false;
map.navigationControl = true;
}
}
function clearMapItems() {
$("#cursor_pred").hide();
if(getAssocSize(map_items) > 0) {
appendDebug("Clearing previous map trace");
for(i in map_items) {
map_items[i].setMap(null);
}
}
map_items = [];
}
function getAssocSize(arr) {
@ -523,23 +413,8 @@ function getAssocSize(arr) {
return i;
}
function initMap(centre_lat, centre_lon, zoom_level) {
// make the map and set center
var latlng = new google.maps.LatLng(centre_lat, centre_lon);
var myOptions = {
zoom: zoom_level,
scaleControl: true,
scaleControlOptions: { position: google.maps.ControlPosition.BOTTOM_LEFT } ,
mapTypeId: google.maps.MapTypeId.TERRAIN,
center: latlng
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
function setupEventHandlers() {
// attach form submit event handler to launch card
// Attach form submit event handler to Run Prediction button
$("#modelForm").ajaxForm({
url: 'ajax.php?action=submitForm',
type: 'POST',
@ -557,26 +432,50 @@ function setupEventHandlers() {
} else if ( data.valid == "true" ) {
predSub();
appendDebug("The server accepted the form data");
// update the global current_uuid variable
// Update the global current_uuid variable
current_uuid = data.uuid;
appendDebug("The server gave us uuid:<br>" + current_uuid);
appendDebug("Starting to poll for progress JSON");
handlePred(current_uuid);
} else {
appendDebug("data.valid was not a recognised state: " + data.valid);
appendDebug("data.valid was not a recognised state: "
+ data.valid);
}
}
});
// Activate the "Save" button in the "Save Location to Cookie" window
// Location saving to cookies event handlers
$("#req_sub_btn").click(function() {
saveLocationToCookie();
});
$("#cookieLocations").click(function() {
appendDebug("User requested locally saved launch sites");
if ( constructCookieLocationsTable("cusf_predictor") ) {
$("#location_save_local").fadeIn();
}
});
$("#req_open").click(function() {
var lat = $("#lat").val();
var lon = $("#lon").val();
$("#req_lat").val(lat);
$("#req_lon").val(lon);
$("#req_alt").val($("#initial_alt").val());
appendDebug("Trying to reverse geo-code the launch point");
rvGeocode(lat, lon, "req_name");
$("#location_save").fadeIn();
})
$("#req_close").click(function() {
$("#location_save").fadeOut();
});
$("#locations_close").click(function() {
$("#location_save_local").fadeOut();
});;
// Activate the "Set with Map" link
$("#setWithClick").click(function() {
setLatLonByClick(true);
});
// Activate the "use burst calc" links
$("#burst-calc-show").click(function() {
$("#burst-calc-wrapper").show();
@ -602,23 +501,26 @@ function setupEventHandlers() {
$("#burst-calc-constants").slideUp();
$("#burst-calc").slideDown();
});
// attach onchange handlers to the lat/long boxes
// Launch card parameter onchange event handlers
$("#lat").change(function() {
plotClick();
});
$("#lon").change(function() {
plotClick();
});
$("#site").change(function() {
if ( $("#site").val() == "Other" ) {
appendDebug("User requested locally saved launch sites");
if ( constructCookieLocationsTable("cusf_predictor") ) {
$("#location_save_local").fadeIn();
}
} else {
plotClick();
}
$("#delta_lat").change(function() {
drawDeltaSquare(map);
});
$("#delta_lon").change(function() {
drawDeltaSquare(map);
});
$("#site").change(function() {
changeLaunchSite();
});
// Controls in the Scenario Information window
$("#showHideDebug").click(function() {
toggleWindow("scenario_template", "showHideDebug", "Show Debug", "Hide Debug");
});
@ -632,12 +534,7 @@ function setupEventHandlers() {
$("#closeErrorWindow").click(function() {
$("#error_window").fadeOut();
});
$("#cookieLocations").click(function() {
appendDebug("User requested locally saved launch sites");
if ( constructCookieLocationsTable("cusf_predictor") ) {
$("#location_save_local").fadeIn();
}
});
$("#about_window_show").click(function() {
$("#about_window").dialog({
modal:true,
@ -649,54 +546,16 @@ function setupEventHandlers() {
}
});
});
$("#delta_lat").change(function() {
drawDeltaSquare(map);
});
$("#delta_lon").change(function() {
drawDeltaSquare(map);
});
$("#site").change(function() {
changeLaunchSite();
});
$("#req_close").click(function() {
$("#location_save").fadeOut();
});
$("#locations_close").click(function() {
$("#location_save_local").fadeOut();
});
$("#req_open").click(function() {
var lat = $("#lat").val();
var lon = $("#lon").val();
$("#req_lat").val(lat);
$("#req_lon").val(lon);
$("#req_alt").val($("#initial_alt").val());
appendDebug("Trying to reverse geo-code the launch point");
rvGeocode(lat, lon, "req_name");
$("#location_save").fadeIn();
});
// Tipsylink tooltip class activation
$(".tipsyLink").tipsy({fade: true});
// Add the onmove event handler to the map canvas
google.maps.event.addListener(map, 'mousemove', function(event) {
showMousePos(event.latLng);
});
}
function rvGeocode(lat, lon, fillField) {
var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(parseFloat(lat), parseFloat(lon));
var coded = "Unnamed";
geocoder.geocode({'latLng': latlng}, function(results, status) {
if ( status == google.maps.GeocoderStatus.OK ) {
// Successfully got rv-geocode information
appendDebug("Got a good response from the geocode server");
coded = results[1].address_components[1].short_name;
} else {
appendDebug("The rv-geocode failed: " + status);
}
// Now write the value to the field
$("#"+fillField+"").val(coded);
});
}
function POSIXtoHM(timestamp, format) {
// using JS port of PHP's date()
var ts = new Date();
@ -712,19 +571,3 @@ function POSIXtoHM(timestamp, format) {
rad = function(x) {return x*Math.PI/180;}
distHaversine = function(p1, p2, precision) {
var R = 6371; // earth's mean radius in km
var dLat = rad(p2.lat() - p1.lat());
var dLong = rad(p2.lng() - p1.lng());
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) * Math.sin(dLong/2) * Math.sin(dLong/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
if ( precision == null ) {
return d.toFixed(3);
} else {
return d.toFixed(precision);
}
}