Sobatcoding.com - Cara Menghitung Jarak Antara Dua Titik Koordinat di PHP
Artikel kali ini membahas tentang cara menghitung jarak antara dua titik koordinat di PHP.
Pertama-tama kita buat dulu sebuah file php misalkan index.php dan sebuah function bernama getDistanceBetweenPoints
seperti pada gambar berikut :
//Source : https://gist.github.com/LucaRosaldi/5676464
function getDistanceBetweenPoints($lat1, $lon1, $lat2, $lon2) {
$theta = $lon1 - $lon2;
$miles = (sin(deg2rad($lat1)) * sin(deg2rad($lat2))) + (cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)));
$miles = acos($miles);
$miles = rad2deg($miles);
$miles = $miles * 60 * 1.1515;
$feet = $miles * 5280;
$yards = $feet / 3;
$kilometers = $miles * 1.609344;
$meters = $kilometers * 1000;
return compact('miles','feet','yards','kilometers','meters');
}
Untuk pengujian kita masukkan parameter ke dalam function seperti pada gambar :
$point1 = array("lat" => -7.82504, "long" => 112.71610);
$point2 = array("lat" => 7.82552, "long" => 114.27800 );
$distance = getDistanceBetweenPoints($point1['lat'], $point1['long'], $point2['lat'], $point2['long']);
print_r($distance)
Untuk source lengkapnya seperti pada gambar berikut:
<?php
/**
* Source : https://gist.github.com/LucaRosaldi/5676464
*/
/**
* Calculates the distance between two points, given their
* latitude and longitude, and returns an array of values
* of the most common distance units
*
* @param {coord} $lat1 Latitude of the first point
* @param {coord} $lon1 Longitude of the first point
* @param {coord} $lat2 Latitude of the second point
* @param {coord} $lon2 Longitude of the second point
* @return {array} Array of values in many distance units
*/
function getDistanceBetweenPoints($lat1, $lon1, $lat2, $lon2) {
$theta = $lon1 - $lon2;
$miles = (sin(deg2rad($lat1)) * sin(deg2rad($lat2))) + (cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)));
$miles = acos($miles);
$miles = rad2deg($miles);
$miles = $miles * 60 * 1.1515;
$feet = $miles * 5280;
$yards = $feet / 3;
$kilometers = $miles * 1.609344;
$meters = $kilometers * 1000;
return compact('miles','feet','yards','kilometers','meters');
}
function dd($str)
{
echo '<pre>';
print_r($str);
echo '</pre>';
die();
}
$point1 = array("lat" => -7.82504, "long" => 112.71610);
$point2 = array("lat" => 7.82552, "long" => 114.27800 );
$distance = getDistanceBetweenPoints($point1['lat'], $point1['long'], $point2['lat'], $point2['long']);
dd($distance);
Untuk hasilnya jika diakses akan muncul seperti pada gambar berikut:
Array
(
[miles] => 1086.6351946979
[feet] => 5737433.8280052
[yards] => 1912477.9426684
[kilometers] => 1748.769830776
[meters] => 1748769.830776
)
Hasil perhitungan muncul dalam beberapa satuan yaitu miles, feet, yards, kilometer ataupun meter. Pilih sesuai dengan yang kalian butuhkan.
Selamat mencoba!
Sekian tutorial kali ini. Jika teman-teman memiliki pertanyaan atau saran mengenai artikel ini, jangan ragu untuk meninggalkan komentar pada form di bawah ini.
Semoga bermanfaat.
Komentar 0