+34 649 382 965 · 4.7 / 5 Read our Google reviews · contacto@phpninja.info
Other

Calculating the distance between two points using latitude and longitude

How to handle distance bettween latitude/longitude points. Haversine Formula http://en.wikipedia.org/wiki/Haversine_formula Php Function to calculate distance between 2 points:

function distance($lat1, $lon1, $lat2, $lon2) { 
$pi80 = M_PI / 180; 
$lat1 *= $pi80; 
$lon1 *= $pi80; 
$lat2 *= $pi80; 
$lon2 *= $pi80; 
$r = 6372.797; // mean radius of Earth in km 
$dlat = $lat2 - $lat1; 
$dlon = $lon2 - $lon1; 
$a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlon / 2) * sin($dlon / 2); 
$c = 2 * atan2(sqrt($a), sqrt(1 - $a)); 
$km = $r * $c; 
//echo ' '.$km; 
return $km; 
}
 

Mysql Query getting the nearest points (objects,products,users, what ever ….) This SlideShare could help you optimize your querys, since make querys and manipulating coords are time consuming with big db’s.

Geo Distance Search with MySQL by Oleksiy Kovyrin

Doing some testing before coding anything http://www.movable-type.co.uk/scripts/latlong.html    

Beto López
Full stack website developer focused in bug fixing and maintenance of Wordpress, Prestashop, HTML, CSS, Javascript, Php y Mysql. Open source contributor too. Beto Ayesa, Linkedin y Twitter.


Calculating the distance between two points using latitude and longitude
Te respondemos rapidamente
Get in touch