PHP calculate Distance Between Two Lat Long - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP calculate Distance Between Two Lat Long - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Sunday, January 7, 2018

PHP calculate Distance Between Two Lat Long

PHP calculate Distance Between Two Lat Long

PHP calculate distance between two lat lng

 

Here is the simple function to get The Distance Between Two Lat Long

<?php

function distance($lat1, $lon1, $lat2, $lon2, $unit) {

  $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;
  $unit = strtoupper($unit);

  if ($unit == "K") {
    return ($miles * 1.609344);
  } else if ($unit == "N") {
      return ($miles * 0.8684);
    } else {
        return $miles;
      }
}

 

echo distance('21.9', '73.2', '22.1','77.2','K');


?>

No comments:

Post a Comment

Post Top Ad