Calculate distances between two coordinates

Asked

Viewed 12,215 times

8


I am using this function below to calculate the difference between two coordinates. Through google maps is reported a difference of 2 to 3km. But the function is returning me the result 56.480188542761 Km.
I wonder if there is something wrong with the function or if there is any other calculation to be made.

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 />";
  • 2

    Just so I understand better, K = kilometer, and N?

  • N is for nautical miles (given the conversion factor used).

  • Who knows!? : ) Forget about N. I’ll only need miles.

  • Another thing @Rafaelsoufraz, the distance that google maps informs, considers the streets to travel, and when you use Haversine’s formula, it calculates the distance in a straight line.

  • I know that. But it still doesn’t justify the function result. 56.480188542761 Km

  • Try with the answer I put.

  • 1

    2 responses with different result values, beware of screwing up.

Show 2 more comments

3 answers

9


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, 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
  • I used this same formula, to find the nearest points in a straight line, within a register of cities, and after that, trace the distance of the routes of the nearest cities, already with coordinates established with the API of googlemaps. Have a look: https://gist.github.com/aymone/00c4e4e8be907bdedda7

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

  • Because you withdrew your result?

  • So, the original formula is in degrees, so it makes no sense to calculate the delta in radians, at least I understand this way, I could be wrong.

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 coordinate A.

4, 3 is coordinate B.

The first value of the coordinate is X and the second is Y;:

`(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 http://www.brasilescola.com/matematica/distancia-entre-dois-pontos.htm

  • Thank you. You explained how to calculate but the calculation is already being done. I just need to understand the final result and either convert it.

  • 6

    That’s on a plane, but the Earth is spherical (or almost spherical).

  • Yeah, I hadn’t noticed,.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.