Calculate Distance between two location with Latitude Longitude using PHP


In this article, we will be writing a simple PHP function to calculate distance between two points with Latitude and Longitude. The distance is then converted into various units including miles, feet, yards, kilometers and meters.

<h2 style="text-align: center;">Calculate Distance between to location with Latitude Longitude using PHP</h2>


function calculateDistance($lat1, $long1, $lat2, $long2){
  $theta = $long1 - $long2;
  $miles = (sin(deg2rad($lat1))) * sin(deg2rad($lat2)) + (cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)));
  $miles = acos($miles);
  $miles = rad2deg($miles);
  $result['miles'] = $miles * 60 * 1.1515;
  $result['feet'] = $result['miles']*5280;
  $result['yards'] = $result['feet']/3;
  $result['kilometers'] = $result['miles']*1.609344;
  $result['meters'] = $result['kilometers']*1000;
  return $result;

echo '<pre>';

Follow this video for complete guidance :

ALSO READ  Cryptocurrency : Get Current Bitcoin Price using PHP API

Comments are closed.