title: How to calculate the distance between two latitude and longitude
body: <p>I'm trying to calculate the distance between Vancouver and Toronto using their latitude and longitudes. I'm using <strong>haversine</strong> formula. I'm expecting a distance of around 4390 km. Can someone tell me what mistake I'm making. Here is my code:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt; 
  &lt;html&gt; 
  &lt;head&gt; 
  &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body onload="getDistance()"&gt;
  &lt;script&gt;
      // 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);
      }       
   &lt;/script&gt;
   &lt;/body&gt;
   &lt;/html&gt;
  </code></pre>
  
  <p>When I run this code, I get quite different numbers.</p>