-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.php
79 lines (68 loc) · 2.06 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
* Contoh dibuat oleh @ibnux
*/
$lat = -6.3730914;
$lon = 106.7116703;
$wilayah = json_decode(file_get_contents("https://ibnux.github.io/BMKG-importer/cuaca/wilayah.json"),true);
$jml = count($wilayah);
// hitung jarak
for($n=0;$n<$jml;$n++){
$wilayah[$n]['jarak'] = distance($lat, $lon, $wilayah[$n]['lat'], $wilayah[$n]['lon'], 'K');
}
//urutkan
usort($wilayah, 'urutkanJarak');
//ambil 5 besar aja
echo "<pre>";
echo "\n<h2>Urutkan dari yang terdekat<br>$lat,$lon</h2>\n";
echo "\n";
for($n=0;$n<5;$n++){
print_r($wilayah[$n]);
echo "\n";
}
echo "\n<h2>";
echo $wilayah[0]['propinsi'].",".$wilayah[0]['kota'].",".$wilayah[0]['kecamatan']."\n";
echo number_format($wilayah[0]['jarak'],2,",",".")." km</h2>\n";
echo "\n";
//ambil cuaca kota terdekat
$json = json_decode(file_get_contents("https://ibnux.github.io/BMKG-importer/cuaca/".$wilayah[0]['id'].".json"),true);
$time = time();
$n = 0;
echo '<table border="1"><tr>';
foreach($json as $cuaca){
$timeCuaca = strtotime($cuaca['jamCuaca']);
//yang lewat ngga perlu ditampilkan
if($timeCuaca>$time){
echo '<td>';
echo '<img src="https://ibnux.github.io/BMKG-importer/icon/'.$cuaca['kodeCuaca'].'.png" class="image">';
echo '<p>'.$cuaca['cuaca'].'</p>';
echo "</td>\n";
}
}
echo '</tr><table>';
echo "\n";
print_r($json);
function urutkanJarak($a, $b) {
return $a['jarak'] - $b['jarak'];
}
// https://www.geodatasource.com/developers/php
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
if (($lat1 == $lat2) && ($lon1 == $lon2)) {
return 0;
}
else {
$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;
}
}
}