<?php
   
//geolocation.php
   //
   //Provides functions to determine distance using latitude and longitude
   //coordinates.
   //
   //Written by Jim Cortez (http://jimcortez.com)
   //Use however you want, let me know if you use it in something useful!
   
   
class GPSCoordinate{
      protected 
$latitude;
      protected 
$longitude;
      
      function 
_construct($lat$long$objPointer=null){
         
$this->latitude $lat;
         
$this->longitude $long;
      }
      
      
//expects another instance of Coordinate
      //used http://www.meridianworlddata.com/Distance-Calculation.asp for formula
      
public function distanceTo($that$units="nautical miles"){
         if(!(
$that instanceof GPSCoordinate)){
            throw new 
Exception("param not a GPSCoordinate: ".$that);
         }
         
$distance = array("nautical miles"=>3437.74677,
                       
"kilometers"=>6378.7
                       
"statute miles"=>3963.0);
         return 
$distance[$units] * acos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 lon1));
      }
      
      
//returns array with coordinates within $minDist distance
      //$minDist should be a floating point in miles
      
public function withinRange($minDist$coords=array()){
         
$within = array();
         foreach(
$coords as $coord){
            if(
$that instanceof GPSCoordinate && $coord.distanceTo($coord) <= $minDist){
               
array_push($within$coord);
            }
         }
         
         return 
$within;
      }
   }
   
?>