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>


<?php


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>';
var_dump(calculateDistance(88,22,77,44));

Follow this video for complete guidance :



Recommended For You

About the Author: Admin