free hit counter

Calculate Distance by Latitude and Longitude using PHP and Javascript

Calculate Distance by Latitude and Longitude using PHP and Javascript

Calculate Distance by Latitude and Longitude using PHP and Javascript

Geo-location is becoming more and more popular as time passes and technology supports it better more so as there is plenty of free resources to help you get the data you need to be more GEO centric with your users. However sometimes you want to give a range from point A to point B and this is one way you can do it. You can do the calculation in this function to return Miles (default), Nautical Miles, or Kilometers just enter the longitude/latitude of point A and do the same for point B and your good to go.

 

This PHP function calculates the distance between to pairs of latitude longitude coordinates. Returns the distance in miles or kilometers.

 

Code (PHP)

  1. <?php  
  2.   
  3. /*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/  
  4. /*::                                                                         :*/
  5. /*::  Passed to function:                                                    :*/  
  6. /*::    lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees)  :*/  
  7. /*::    lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees)  :*/  
  8. /*::    unit = the unit you desire for results                               :*/  
  9. /*::           where: ‘M’ is statute miles                                   :*/  
  10. /*::                  ‘K’ is kilometers (default)                            :*/  
  11. /*::                  ‘N’ is nautical miles                                  :*/  
  12. /*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
  13. function distance($lat1$lon1$lat2$lon2$unit) {  
  14.   
  15.   $theta = $lon1 – $lon2;  
  16.   $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));  
  17.   $dist = acos($dist);  
  18.   $dist = rad2deg($dist);  
  19.   $miles = $dist * 60 * 1.1515;  
  20.   $unit = strtoupper($unit);  
  21.   
  22.   if ($unit == “K”) {  
  23.     return ($miles * 1.609344);  
  24.   } else if ($unit == “N”) {  
  25.       return ($miles * 0.8684);  
  26.     } else {  
  27.         return $miles;  
  28.       }  
  29. }  
  30.   
  31. echo distance(32.9697, -96.80322, 29.46786, -98.53506, “M”) . ” Miles<br>”;  
  32. echo distance(32.9697, -96.80322, 29.46786, -98.53506, “K”) . ” Kilometers<br>”;  
  33. echo distance(32.9697, -96.80322, 29.46786, -98.53506, “N”) . ” Nautical Miles<br>”;  
  34.   
  35. ?>  

 

Code (Javascript)

  1. function distance(lat1, lon1, lat2, lon2, unit) {  
  2.     var radlat1 = Math.PI * lat1/180  
  3.     var radlat2 = Math.PI * lat2/180  
  4.     var radlon1 = Math.PI * lon1/180  
  5.     var radlon2 = Math.PI * lon2/180  
  6.     var theta = lon1-lon2  
  7.     var radtheta = Math.PI * theta/180  
  8.     var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);  
  9.     dist = Math.acos(dist)  
  10.     dist = dist * 180/Math.PI  
  11.     dist = dist * 60 * 1.1515  
  12.     if (unit==“K”) { dist = dist * 1.609344 }  
  13.     if (unit==“N”) { dist = dist * 0.8684 }  
  14.     return dist  
  15. }           

 

  
Source :  geodatasource.com

Leave a Reply