Miscellaneous Code

  Home arrow Miscellaneous Code arrow Calculate distance based on lat. long
MISCELLANEOUS CODE

Calculate distance based on lat. long
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2005-01-31

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

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

    By : icandothat

    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="$Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <?php
    if( $_POST['Submit']){
    $distance = distance($_POST[LAT1], $_POST[LONG1], $_POST[LAT2],$_POST[LONG2]);

    }


    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

    }

    else{

    $dist = 3963.1* acos(sin($A)*sin($C)+ cos($A)* cos($C)* cos($B-$D));
    }
    return ($dist);
    }


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

    More Miscellaneous Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap