Warning: include_once(analyticstracking.php): failed to open stream: No such file or directory in /www/wwwroot/studentstutorial.com/php-gps/php-distance-from-latitude-longitude.php on line 37

Warning: include_once(): Failed opening 'analyticstracking.php' for inclusion (include_path='.:') in /www/wwwroot/studentstutorial.com/php-gps/php-distance-from-latitude-longitude.php on line 37


Ask Question

Menu

How to get distance from two latitude and longitude in PHP


index.php

<!DOCTYPE html>
<html>
<body>
<form method="post" action="test.php">
Latitude1 :<br>
<input type="text" name="latitude1">
<br>
longitude1:<br>
<input type="text" name="longitude1">
<br>
Latitude2 :<br>
<input type="text" name="latitude2">
<br>
longitude2:<br>
<input type="text" name="longitude2">
<br>
<br>
<input type="submit" name="save" value="submit">
</form>
</body>
</html>

test.php

<?php
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$lat1 = $_POST['latitude1'];
$lon1 = $_POST['longitude1'];
$lat2 = $_POST['latitude2'];
$lon2 = $_POST['longitude2'];
$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("", "", "", "", "K") . " Kilometers<br>";
?>










Subscribe