How to calculate the starting value of the Great Circle method?

I want to calculate the original bearing from point A to point B by the Great Circle method. The javascript prototype I got from the website like:

var y = Math.sin(λ21) * Math.cos(φ2);
var x = Math.cos(φ1)*Math.sin(φ2) -
Math.sin(φ1)*Math.cos(φ2)*Math.cos(λ21);
var brng = Math.atan2(y, x).toDegrees();

      

Where: λ = longitude φ = latitude

So I am trying to use this HTML like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

      

  

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

      

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

      

My input for location 1 => latitude = -7.2742174 and longitude = 112.719087 and My input for location 2 => latitude = -7.586675 and longtitude = 112.8082266

But the problem is that the result on my page is 175.63821414343275. Also, the correct answer is 164.21. Anyone, do you know that my error is in my program? Thank..

+3


source to share





All Articles