Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Map
- {
- const UNIT_MILE = 0;
- const UNIT_KILOMETER = 1;
- const UNIT_NAUTICAL_MILE = 2;
- /**
- * Calculate distance between two coords positions
- *
- * @param float $lat1 Latitude of the first coord
- * @param float $lon1 Longitude of the first coord
- * @param float $lat2 Latitude of the second coord
- * @param float $lon2 Longitude of the second coord
- * @param int $unit Unit of measure of the returned value
- *
- * @return float Distance measured
- */
- public function distance($lat1, $lon1, $lat2, $lon2, $unit = self::UNIT_MILE)
- {
- $theta = $lon1 - $lon2;
- $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2))
- + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
- $dist = acos($dist);
- $dist = rad2deg($dist);
- $miles = $dist * 60 * 1.1515;
- switch ($unit) {
- case self::UNIT_KILOMETER:
- return ($miles * 1.609344);
- break;
- case self::UNIT_NAUTICAL_MILE:
- return ($miles * 0.8684);
- break;
- case self::UNIT_MILE:
- default:
- return $miles;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment