WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Vincenty formula to calculate distance in KM
Categories : PHP, Math., Algorithms Click here to Update Your Picture
Digital Human
Date : Sep 16th 2005
Grade : 3 of 5 (graded 6 times)
Viewed : 11184
File : 4226.php
Images : No Images for this code example.
Search : More code by Digital Human
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

<?
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.