var y = Math.sin(λ2-λ1) * Math.cos(φ2); var x = Math.cos(φ1)*Math.sin(φ2) - Math.sin(φ1)*Math.cos(φ2)*Math.cos(λ2-λ1); var brng = Math.atan2(y, x).toDegrees();
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml"> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> <title>Google Maps JavaScript API Example: Extraction of Geocoding Data</title> <script> function bearing() { if (typeof(Number.prototype.toDegrees) === "undefined") { Number.prototype.toDegrees = function() { return this * 180 / Math.PI; } } var getlat1 = document.getElementById("lat1").value; var getlong1 = document.getElementById("long1").value; var getlat2 = document.getElementById("lat2").value; var getlong2 = document.getElementById("long2").value; var lat1 = parseFloat(getlat1); var long1 = parseFloat(getlong1); var lat2 = parseFloat(getlat2); var long2 = parseFloat(getlong2); var y = Math.sin(long2-long1) * Math.cos(lat2); var x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(long2-long1); var brng = Math.atan2(y, x).toDegrees(); document.getElementById('bearing').innerHTML = brng; return brng; } </script> </head> <body> <form action="#" onsubmit="bearing(); return false;"> <p> Location 1 : <input type="text" name="lat1" id="lat1" size="40" /> <input type="text" name="long1" id="long1" size="40" /> </p> <p> Location 2 : <input type="text" name="lat2" id="lat2" size="40" /> <input type="text" name="long2" id="long2" size="40" /> </p> <p> <input type="submit" name="find" value="Search" /> </p> </form> Distance : <p id="results"></p> <br /> Bearing : <p id="bearing"></p> </body> </html>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)