Long / Lat distance calculations???
Posted: Mon Apr 05, 2004 7:41 am
Hello!
I am trying to calculate the distance between two Long/Lat places...
I use the following code, but it won't give me the correct result, anyone know of what I have done wrong?
I have based this on this formula http://jan.ucc.nau.edu/~cvm/latlon_formula.html
(The working calculator on that page is on http://jan.ucc.nau.edu/~cvm/latlongdist.html)
It should give something like 8987 km but I get 6324 km?!
Okay, I noticed an error, but now I get 16021 km!?!?
Any helpers ?? Thanks a bunch!
//Lorens
I am trying to calculate the distance between two Long/Lat places...
I use the following code, but it won't give me the correct result, anyone know of what I have done wrong?
I have based this on this formula http://jan.ucc.nau.edu/~cvm/latlon_formula.html
(The working calculator on that page is on http://jan.ucc.nau.edu/~cvm/latlongdist.html)
Code: Select all
// Define variables for the calculation
$dist = 0; // Distance between source/destination
$rad_km = 6378; // Earth Radius in kilometers
$rad_smi = 3963.1; // Earth Radius in statue miles
$rad_nmi = 3443.9; // Earth Radius in nautical miles
/*
Source Latitude: 373700N -- 37.6166666667
Source Longitude: 1222200W -- 122.366666667
Destination Latitude: 484400N -- 48.7333333333
Destination Longitude: 022300E -- 2.38333333333
*/
$slat_deg = 37;
$slat_min = 37;
$slat_sec = 0;
$slat_ns = "N";
$slon_deg = 122;
$slon_min = 22;
$slon_sec = 0;
$slon_we = "W";
$dlat_deg = 48;
$dlat_min = 44;
$dlat_sec = 0;
$dlat_ns = "N";
$dlon_deg = 2;
$dlon_min = 23;
$dlon_sec = 0;
$dlon_we = "E";
$slat_dec = $slat_deg + ( ( $slat_min + ( $slat_sec / 60 ) ) / 60 );
$slon_dec = $slon_deg + ( ( $slon_min + ( $slon_sec / 60 ) ) / 60 );
$dlat_dec = $dlat_deg + ( ( $dlat_min + ( $dlat_sec / 60 ) ) / 60 );
$dlon_dec = $dlon_deg + ( ( $dlon_min + ( $dlon_sec / 60 ) ) / 60 );
if( $slat_ns == "S" OR $slat_ns == "s" ) {
$slat_dec = $slat_dec * -1;
}
if( $dlat_ns == "S" OR $dlat_ns == "s" ) {
$dlat_dec = $dlat_dec * -1;
}
if( $slon_we == "W" OR $slon_we == "w" ) {
$slon_dec = $slon_dec * -1;
}
if( $dlon_we == "W" OR $dlon_we == "w" ) {
$dlon_dec = $dlon_dec * -1;
}
$a1 = deg2rad( $slat_dec );
$a2 = deg2rad( $slon_dec );
$b1 = deg2rad( $dlat_dec );
$b2 = deg2rad( $dlon_dec ); // I had an error here, but it's still wrong?!
$r = $rad_km;
$dist = acos(cos($a1)*cos($b1)*cos($a2)*cos($b2) + cos($a1)*sin($b1)*cos($a2)*sin($b2) + sin($a1)*sin($a2)) * $r;
echo "<p>Distance: $dist</p>";Okay, I noticed an error, but now I get 16021 km!?!?
Any helpers ?? Thanks a bunch!
//Lorens