/** * very approximate calculation of the distance in kilometers between two coordinates * @param GMapCoord $coord2 * @return float * @author fabriceb * @since 2009-05-03 */ public function distanceFrom($coord2) { $lat_dist = abs($this->getLatitude()-$coord2->getLatitude()); $lng_dist = abs($this->getLongitude()-$coord2->getLongitude()); $rad_dist = deg2rad(sqrt(pow($lat_dist,2)+pow($lng_dist,2))); return $rad_dist * self::EARTH_RADIUS; }
/** * * @param GMapCoord $gmap_coord * @return boolean $is_inside * @author fabriceb * @since Jun 2, 2009 fabriceb */ public function containsGMapCoord(GMapCoord $gmap_coord) { $is_inside = ( $gmap_coord->getLatitude() < $this->getNorthEast()->getLatitude() && $gmap_coord->getLatitude() > $this->getSouthWest()->getLatitude() && $gmap_coord->getLongitude() < $this->getNorthEast()->getLongitude() && $gmap_coord->getLongitude() > $this->getSouthWest()->getLongitude() ); return $is_inside; }
/** * exact distance with Haversine formula * * @param GMapCoord $coord2 * @return float * @see http://www.movable-type.co.uk/scripts/latlong.html * * @author fabriceb * @since Apr 21, 2010 */ public function exactDistanceFrom($coord2) { $lat1 = deg2rad($this->getLatitude()); $lat2 = deg2rad($coord2->getLatitude()); $lon1 = deg2rad($this->getLongitude()); $lon2 = deg2rad($coord2->getLongitude()); $dLatHalf = ($lat2 - $lat1) / 2; $dLonHalf = ($lon2 - $lon1) / 2; $a = pow(sin($dLatHalf), 2) + cos($lat1) * cos($lat2) * pow(sin($dLonHalf), 2); $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); return $c * self::EARTH_RADIUS; }