Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, I want calculate initial bearing from point A to point B on Great Circle Method. The prototype of javascript that I got from website like this :


JavaScript
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();


Where :
λ = longtitude
φ = latitude



So I try it to my HTML like this :


HTML
<!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>


My input for Location 1 => latitude = -7.2742174 & longtitude = 112.719087
My input for Location 2 => latitude = -7.586675 & longtitude = 112.8082266


But the problem is, the result on my page is 175.63821414343275. Besides, the right answer is 164.21.
Anyone, do you know what's my mistake on my program? Thanks..
Posted
Updated 10-Dec-14 15:06pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900