Advertisemen
Here is the map between Sydney and Canberra.
In this tutorial, we are going to learn how to calculate distance between these two cities using PHP.
To start up, create an empty php file and paste these lines of code in it.
<?php
function getDistance($lat1, $lon1, $lat2, $lon2)
{
$radius = 6371; // 1
$rad = doubleval(M_PI/180.0); // 2
$lat1 = doubleval($lat1) * $rad; // 3
$lon1 = doubleval($lon1) * $rad;
$lat2 = doubleval($lat2) * $rad;
$lon2 = doubleval($lon2) * $rad;
$latDiff = $lon2 - $lon1;
$dist = acos(sin($lat1) * sin($lat2) +
cos($lat1) * cos($lat2) * cos($latDiff)); // 4
if ($dist < 0)
{
$dist += M_PI;
}
return $dist = $dist * $radius;
}
// Sydney
$lat1 = -33.8737;
$lon1 = 151.2069;
// Canberra
$lat2 = -35.282;
$lon2 = 149.1287;
$dist = getDistance($lat1, $lon1, $lat2, $lon2);
$formatted = sprintf("%.2f", $dist * 0.621); // 5
echo $formatted." Miles";
?>
Advertisemen
Tidak ada komentar:
Posting Komentar