<?php
/**
* Distance between two points
*
* @param float $lat1 Latitude 1
* @param float $lng1 Longitude 1
* @param float $lat2 Latitude 2
* @param float $lng2 Longitude 2
* @param string $unit [mi|km] miles|kilometers
*
* @return float distance in mi|km. Default is miles.
*/
function harversineDistance($lat1, $lng1, $lat2, $lng2, $unit = 'mi')
{
$latd = deg2rad($lat2 - $lat1);
$lngd = deg2rad($lng2 - $lng1);
$a = sin($latd / 2) * sin($latd / 2) +
cos(deg2rad($lat1)) * cos(deg2rad($lat2)) *
sin($lngd / 2) * sin($lngd / 2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
$radius = ( $unit === 'km' ) ? 6371.009 : 3958.761; // miles
return round($radius * $c, 3);
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.