Calculate distances between two coordinates

8


I am using this function below to calculate the difference between two coordinates. Through google maps is informed a difference of about 2 to 3km. But the function is returning me the result 56.480188542761 Km. I would like to know if there is something wrong with the function or if there is any more calculation to be done.

function distancia($lat1, $lon1, $lat2, $lon2, $unit) {

    $theta = $lon1 - $lon2;
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    $unit = strtoupper($unit);

    if ($unit == "K") {
        return ($miles * 1.609344);
    } else if ($unit == "N") {
        return ($miles * 0.8684);
    } else {
        return $miles;
    }
}

// -12.971683, -38.460108 = bairro pernambues em salvador
// -12.981290, -38.465043 = shopping iguatemi em salvador
echo distancia(-12.971683, -38.460108, -12.981290, -38.981290, "k") . " Km<br />";
    
asked by anonymous 04.07.2014 / 16:31

3 answers

7

Haversine formula applied in PHP

function distancia($lat1, $lon1, $lat2, $lon2) {

$lat1 = deg2rad($lat1);
$lat2 = deg2rad($lat2);
$lon1 = deg2rad($lon1);
$lon2 = deg2rad($lon2);

$dist = (6371 * acos( cos( $lat1 ) * cos( $lat2 ) * cos( $lon2 - $lon1 ) + sin( $lat1 ) * sin($lat2) ) );
$dist = number_format($dist, 2, '.', '');
return $dist;
}

echo distancia(-12.9813346,-38.4653612, -12.9741491,-38.4696483) . " Km<br />";

// 0.92 Km

Or so by checking the Delta.

function distancia($lat1, $lon1, $lat2, $lon2) {

$lat1 = deg2rad($lat1);
$lat2 = deg2rad($lat2);
$lon1 = deg2rad($lon1);
$lon2 = deg2rad($lon2);

$latD = $lat2 - $lat1;
$lonD = $lon2 - $lon1;

$dist = 2 * asin(sqrt(pow(sin($latD / 2), 2) +
cos($lat1) * cos($lat2) * pow(sin($lonD / 2), 2)));
$dist = $dist * 6371;
return number_format($dist, 2, '.', '');
}

echo distancia(-12.9813346,-38.4653612, -12.9741491,-38.4696483) . " Km<br />";

// 0.92 Km
    
04.07.2014 / 17:13
2

Following Haversine's wikipedia

<?php

function distancia($lat1, $lon1, $lat2, $lon2) {
$lat = deg2rad($lat2-$lat1);
$lon = deg2rad($lon2-$lon1);
$t = sin($lat/2) * sin($lat/2) + cos(deg2rad(lat1)) * cos(deg2rad($lat2)) *sin(lon/2) * sin(lon/2);
$l = 2 * atan2(sqrt($t), sqrt(1-$t));
$result = 6371 * $l;
return $result;
}



echo distancia(-12.9813346,-38.4653612, -12.9741491,-38.4696483) . " Km";


?>

Result here was 0.798991145405 Km

    
04.07.2014 / 17:32
0

To calculate the distance between two points, use the following formula:

Square root of: ( Xa - Xb )^2 + ( Ya - Yb )^2

Example

12, 14 is the coordinate A.

4, 3 is the B coordinate.

The first value of the coordinate equals X and the second value to Y, therefore:

'(Xa-Xb)^2+(Ya-Yb)^2' = '(12-4)^2+(14-3)^2'

= '(8)^2+(11)^2'

= '(8)^2+(11)^2'

= '64+121'

The distance between A and B is the square root of 185 or ~ 13.6

Read link

    
04.07.2014 / 17:03