This script/web page lets you calculate Geographical distance based on Latitude and Longitude. It does not compensate for the fact that the earth is fat in the middle. It does not take into account altitude. However, it is accurate enough for most applications.
function distance($Aa, $Ba, $Ca, $Da){ // this is just some basic error checking i'd normally put it into a seperate file but // for the purposes of this file... well you know. // The math for this was taken from http://mathforum.org/library/drmath/view/51711.html $input = array($Aa, $Ba, $Ca, $Da); foreach($input as $name){ if (ereg("[[:alpha:]]",$name)){ echo "You cannot enter letters into this function<br>\n"; die; } if(ereg("\.",$name)){ $dot = "."; $pos = strpos($name, $dot); //echo $pos." <br>\n"; if($pos > 3){ echo "The input cannot exceed more than 3 digits left of the decimal<br>\n"; die; } } if($name > 365){ echo "The input cannot exceed 365 degrees <BR>\n"; die;
} }
$A = $Aa/57.29577951; $B = $Ba/57.29577951; $C = $Ca/57.29577951; $D = $Da/57.29577951; //convert all to radians: degree/57.29577951
if ($A == $C && $B == $D ){ $dist = 0; } else if ( (sin($A)* sin($C)+ cos($A)* cos($C)* cos($B-$D)) > 1){ $dist = 3963.1* acos(1);// solved a prob I ran into. I haven't fully analyzed it yet
?> <body bgcolor="#FFFFFF" text="#000000"> <form name="form1" method="post" action=""> <p>Point one Lat <input type="text" name="LAT1" value="<?=$_POST[LAT1]?>" > Long <input type="text" name="LONG1" value="<?=$_POST[LONG1]?>"> </p> <p>Point two Lat <input type="text" name="LAT2" value="<?=$_POST[LAT2]?>"> Long <input type="text" name="LONG2" value="<?=$_POST[LONG2]?>"> </p> <p> <input type="submit" name="Submit" value="Submit"> </p> </form> <? echo "<br>distance = ".$distance." miles (as the crow flies)<br>"; ?> </body> </html>
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.