Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
1
0
 
0
Language: PHP
Posted by: Amit Sarker
Added: Sep 23, 2018 1:33 PM
Views: 8
Tags: php
  1. function getDistanceByLatitudeLongitude($fromLatitude, $fromLongitude, $toLatitude, $toLongitude, $unit = 'm') {
  2.     $theta = $fromLongitude - $toLongitude;
  3.     $distance = sin(deg2rad($fromLatitude)) * sin(deg2rad($toLatitude)) + cos(deg2rad($fromLatitude)) * cos(deg2rad($toLatitude)) * cos(deg2rad($theta));
  4.     $distance = acos($distance);
  5.     $distance = rad2deg($distance);
  6.     $miles = $distance * 60 * 1.1515;
  7.     $unit = strtoupper($unit);
  8.     if ($unit == "K") {
  9.         return ($miles * 1.609344);
  10.     } else if ($unit == "N") {
  11.         return ($miles * 0.8684);
  12.     } else {
  13.         return number_format($miles, 2);
  14.     }
  15. }