<?
function CalculateDist ( $lat1 , $lat2 , $lon1 , $lon2 ){
$a = 6378137 - 21 * sin ( lat );
$b = 6356752.3142 ;
$f = 1 / 298.257223563 ;
$p1_lat = $lat1 / 57.29577951 ;
$p2_lat = $lat2 / 57.29577951 ;
$p1_lon = $lon1 / 57.29577951 ;
$p2_lon = $lon2 / 57.29577951 ;
$L = $p2_lon - $p1_lon ;
$U1 = atan (( 1 - $f ) * tan ( $p1_lat ));
$U2 = atan (( 1 - $f ) * tan ( $p2_lat ));
$sinU1 = sin ( $U1 );
$cosU1 = cos ( $U1 );
$sinU2 = sin ( $U2 );
$cosU2 = cos ( $U2 );
$lambda = $L ;
$lambdaP = 2 * PI ;
$iterLimit = 20 ;
while( abs ( $lambda - $lambdaP ) > 1e-12 && $iterLimit > 0 ) {
$sinLambda = sin ( $lambda );
$cosLambda = cos ( $lambda );
$sinSigma = sqrt (( $cosU2 * $sinLambda ) * ( $cosU2 * $sinLambda ) + ( $cosU1 * $sinU2 - $sinU1 * $cosU2 * $cosLambda ) * ( $cosU1 * $sinU2 - $sinU1 * $cosU2 * $cosLambda ));
//if ($sinSigma==0){return 0;} // co-incident points
$cosSigma = $sinU1 * $sinU2 + $cosU1 * $cosU2 * $cosLambda ;
$sigma = atan2 ( $sinSigma , $cosSigma );
$alpha = asin ( $cosU1 * $cosU2 * $sinLambda / $sinSigma );
$cosSqAlpha = cos ( $alpha ) * cos ( $alpha );
$cos2SigmaM = $cosSigma - 2 * $sinU1 * $sinU2 / $cosSqAlpha ;
$C = $f / 16 * $cosSqAlpha *( 4 + $f *( 4 - 3 * $cosSqAlpha ));
$lambdaP = $lambda ;
$lambda = $L + ( 1 - $C ) * $f * sin ( $alpha ) * ( $sigma + $C * $sinSigma *( $cos2SigmaM + $C * $cosSigma *(- 1 + 2 * $cos2SigmaM * $cos2SigmaM )));
}
// if ($iterLimit==0) {
// return "Niks"; // formula failed to converge
// }
$uSq = $cosSqAlpha *( $a * $a - $b * $b )/( $b * $b );
$A = 1 + $uSq / 16384 *( 4096 + $uSq *(- 768 + $uSq *( 320 - 175 * $uSq )));
$B = $uSq / 1024 * ( 256 + $uSq *(- 128 + $uSq *( 74 - 47 * $uSq )));
$deltaSigma = $B * $sinSigma *( $cos2SigmaM + $B / 4 *( $cosSigma *(- 1 + 2 * $cos2SigmaM * $cos2SigmaM )- $B / 6 * $cos2SigmaM *(- 3 + 4 * $sinSigma * $sinSigma )*(- 3 + 4 * $cos2SigmaM * $cos2SigmaM )));
$s = $b * $A *( $sigma - $deltaSigma );
return $s / 1000 ;
}
echo CalculateDist ( $lat1 , $lat2 , $lon1 , $lon2 );
?>
Replace $lat1 with lat of point 1. eg. 52.2166667
Replace $lat2 with lat of point 2. eg. 52.35
Replace $lon1 with lon of point 1. eg. 5.9666667
Replace $lon2 with lon of point 2. eg. 4.9166667
Should give 73.174873 (or higher).
Have fun and don't forget to vote for me !!!
Prime number finder (Sieve of Erastothenes) Categories : PHP , Algorithms , Math. Library of math functions to expand the functionality of PHP3. Version 1.2.1 fixes a major problem with the gcd function.
Categories : Algorithms , PHP , Math. Diffusion-Limited Aggregation visualization Categories : PHP , Graphics , Algorithms , Math. Reverse a given number Categories : PHP , Beginner Guides , Algorithms , Math. Show the steps for converting a number from a given base to base 10. Shows the steps involved in converting a number from a given base to base 10. Categories : PHP , Math. , Algorithms How to judge if an integer is odd or is even in Php3? Categories : Math. , PHP , Algorithms quick sort for associative arrays Categories : Algorithms , Arrays , PHP Credit Card Identification and Validation Class - The credit_card class provides methods for cleaning, validating and identifying the type of credit card numbers. Categories : PHP , PHP Classes , Credit Cards , Ecommerce , Algorithms Prime Spiral is a image plotted with all the primes in a number spiral.
Categories : Algorithms , Graphics , GD image library , Math. Dollar Serial Number Validator Categories : PHP , Security , Algorithms Boolean Keyword Interpreter Categories : PHP , Algorithms , Search Engines Math operations on big numbers Categories : PHP , Math. Latitude-Longitude to Miles Categories : PHP , Utilities , Math. A recursive function to traverse a multi-dimensional array where the
dimensions are not known Categories : Arrays , PHP , Algorithms The Porter Word Stemming Algorithm in PHP
Reduces words to their base stem for search engines and indexing Categories : Algorithms , PHP , Strings
John Doe wrote : 1882
There are a few problems with this, minor and major.
$a = 6378137 - 21 * sin(lat);
//should be (I guess...)
$a = 6378137 - 21 * sin($lat1);
// I have to say, I don't understand this line - and certainly it doesn't match up to the javascript function here http://www.movable-type.co.uk/scripts/latlong-vincenty.html
//The major one is, if any one of the lon or lat values are the same, so you travelled along an exact line say 100m, it returns 0m.
John Doe wrote : 1883
Oh I forgot to add, this obviously only calculates flat distance, not surface distance.
If you have elevation data, it is easily modified, I would post it, but the original code is flawed so far.