0

I'm trying to calculate the distance between Vancouver and Toronto using their latitude and longitudes. I'm using haversine formula. I'm expecting a distance of around 4390 km. Can someone tell me what mistake I'm making. Here is my code:

<!DOCTYPE html> 
<html> 
<head> 
<title></title>
</head>
<body onload="getDistance()">
<script>
    // Convert Degress to Radians
    function Deg2Rad( deg ) {
       return deg * Math.PI / 180;
    }

    function getDistance()
    {       
        //Toronto Latitude  43.74 and longitude  -79.37
        //Vancouver Latitude  49.25 and longitude  -123.12
        lat1 = Deg2Rad(43.74); 
        lat2 = Deg2Rad(49.25); 
        lon1 = Deg2Rad(-79.37); 
        lon2 = Deg2Rad(-123.12);
        latDiff = lat2-lat1;
        lonDiff = lon2-lon1;
        var R = 6371000; // metres
        var φ1 = lat1;
        var φ2 = lat2;
        var Δφ = Deg2Rad(latDiff);
        var Δλ = Deg2Rad(lonDiff);

        var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
                Math.cos(φ1) * Math.cos(φ2) *
                Math.sin(Δλ/2) * Math.sin(Δλ/2);
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

        var d = R * c;
        alert('d: ' + d);

        var dist = Math.acos( Math.sin(φ1)*Math.sin(φ2) + Math.cos(φ1)*Math.cos(φ2) * Math.cos(Δλ) ) * R;
        alert('dist: ' + dist);
    }       
 </script>
 </body>
 </html>

When I run this code, I get quite different numbers.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    Possible duplicate of [Calculate distance between two latitude-longitude points? (Haversine formula)](http://stackoverflow.com/questions/27928/calculate-distance-between-two-latitude-longitude-points-haversine-formula) – Teemu Mar 11 '16 at 20:23
  • Result of the above code doesn't produce correct numbers and I need some help. I know Haversine question has been asked here before. –  Mar 11 '16 at 20:34

1 Answers1

1

I think you should expect more like 3358km. http://www.distancefromto.net/distance-from/Toronto/to/Vancouver

Here is a fiddle with the correct result: https://jsfiddle.net/Lk1du2L1/

And in that case, you'll be correct if you'd remove your deg2rad form your secondary results - you're doing it too often:

 <!DOCTYPE html> 
<html> 
<head> 
<title></title>
</head>
<body onload="getDistance()">
<script>
    // Convert Degress to Radians
    function Deg2Rad( deg ) {
       return deg * Math.PI / 180;
    }

    function getDistance()
    {       
        //Toronto Latitude  43.74 and longitude  -79.37
        //Vancouver Latitude  49.25 and longitude  -123.12
        lat1 = Deg2Rad(43.74); 
        lat2 = Deg2Rad(49.25); 
        lon1 = Deg2Rad(-79.37); 
        lon2 = Deg2Rad(-123.12);
        latDiff = lat2-lat1;
        lonDiff = lon2-lon1;
        var R = 6371000; // metres
        var φ1 = lat1;
        var φ2 = lat2;
        var Δφ = latDiff;
        var Δλ = lonDiff;

        var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
                Math.cos(φ1) * Math.cos(φ2) *
                Math.sin(Δλ/2) * Math.sin(Δλ/2);
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

        var d = R * c;
        alert('d: ' + d);

        var dist = Math.acos( Math.sin(φ1)*Math.sin(φ2) + Math.cos(φ1)*Math.cos(φ2) * Math.cos(Δλ) ) * R;
        alert('dist: ' + dist);
    }       
 </script>
 </body>
 </html>
dmgig
  • 4,400
  • 5
  • 36
  • 47
  • I know, I can't find the mistake I'm making in the code. When I run the code, I get quite a different number 85568.6616. –  Mar 11 '16 at 20:37
  • look at the code I posted. When I run you code with those changes, I get the correct distance: `d: 3355945.3482` – dmgig Mar 11 '16 at 20:38
  • You already converted deg2rad when you created your lat1 & lat2 etc variables. When you do it again, you are changing your values and getting the incorrect `85568.661` number. – dmgig Mar 11 '16 at 20:40
  • I changed my code. Removed the duplicate and now I get dist: 615456.6054867743 which doesn't look correct either. –  Mar 11 '16 at 20:45
  • I reposted the whole code. When I run this, I get `3355945.3482` – dmgig Mar 11 '16 at 20:46
  • when i added this code in my angularjs project i am getting illegal character error while compiling. – Shamseer PC May 15 '18 at 06:05
  • maybe it is having trouble with those greek characters? – dmgig May 15 '18 at 21:13
  • 1
    Yes greek characters are making trouble. when i replace it with the normal character it is working. – Shamseer PC May 17 '18 at 03:33