« Back to Index

Calculate distance between two co-ordinates on a Map (Lat/Lng)

View original Gist on GitHub

Tags: #js

calc.js

origin = new google.maps.LatLng(userLatLng[0], userLatLng[1]);
destination = new google.maps.LatLng(results[0].geometry.location.Qa, results[0].geometry.location.Ra);

var pi, 
    custlat1 = userLatLng[0],
    custlong1 = userLatLng[1],
    custlat2 = results[0].geometry.location.Qa,
    custlong2 = results[0].geometry.location.Ra,
    r,
    dlat,
    dlng,
    a,
    c,
    km,
    miles;
						
// Calculate the distance between two Lat/Lng values
pi = Math.PI / 180;
custlat1 *= pi;
custlong1 *= pi;
custlat2 *= pi;
custlong2 *= pi;
r = 6372.797;
dlat = custlat2 - custlat1;
dlng = custlong2 - custlong1;
a = Math.sin(dlat / 2) * Math.sin(dlat / 2) + Math.cos(custlat1) * Math.cos(custlat2) * Math.sin(dlng / 2) * Math.sin(dlng / 2);
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
km = roundNumber(r * c, 2); // Distance in KM so we can work out how the milage
miles = roundNumber(km * 0.621371192, 2); // Now we have the distance in miles
					
console.log('miles:', miles, 'address:', address);

calc.php

$custlat1 = xxxxx;
$custlong1 = xxxxx;
$custlat2 = xxxxx;
$custlong2 = xxxxx;

$pi80 = M_PI / 180;
$custlat1 *= $pi80;
$custlong1 *= $pi80;
$custlat2 *= $pi80;
$custlong2 *= $pi80;
$r = 6372.797; 
$dlat = $custlat2 - $custlat1;
$dlng = $custlong2 - $custlong1;
$a = sin( $dlat / 2 ) * sin( $dlat / 2 ) + cos( $custlat1 ) * cos( $custlat2 ) * sin( $dlng / 2 ) * sin( $dlng / 2 );
$c = 2 * atan2( sqrt( $a ), sqrt( 1 - $a ) );
  
// Distance in KM so we can work out how the milage
$km = round( $r * $c, 2 );
  
// Now we have the distance in miles
$miles = round( $km * 0.621371192, 2 );