45 lines
1.6 KiB
PHP
45 lines
1.6 KiB
PHP
<?php
|
|
// ================================================================
|
|
// API: Reverse Geocoding Proxy (Nominatim)
|
|
// ================================================================
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
header('Access-Control-Allow-Origin: *');
|
|
|
|
$lat = isset($_GET['lat']) ? (float)$_GET['lat'] : null;
|
|
$lng = isset($_GET['lng']) ? (float)$_GET['lng'] : null;
|
|
|
|
if (!$lat || !$lng) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'lat dan lng wajib']);
|
|
exit;
|
|
}
|
|
|
|
$url = "https://nominatim.openstreetmap.org/reverse?format=json&lat={$lat}&lon={$lng}&addressdetails=1&accept-language=id";
|
|
|
|
$opts = ['http' => ['header' => "User-Agent: WebGIS-PovertyMap/2.0 (https://github.com)\r\n", 'timeout' => 10]];
|
|
$context = stream_context_create($opts);
|
|
$result = @file_get_contents($url, false, $context);
|
|
|
|
if ($result === false) {
|
|
echo json_encode(['display_name' => "{$lat}, {$lng}", 'fallback' => true]);
|
|
exit;
|
|
}
|
|
|
|
$data = json_decode($result, true);
|
|
$addr = $data['display_name'] ?? "{$lat}, {$lng}";
|
|
|
|
// Ekstrak komponen alamat
|
|
$components = $data['address'] ?? [];
|
|
$kelurahan = $components['suburb'] ?? $components['village'] ?? $components['neighbourhood'] ?? '';
|
|
$kecamatan = $components['city_district'] ?? $components['district'] ?? '';
|
|
$kota = $components['city'] ?? $components['town'] ?? $components['county'] ?? '';
|
|
|
|
echo json_encode([
|
|
'display_name' => $addr,
|
|
'kelurahan' => $kelurahan,
|
|
'kecamatan' => $kecamatan,
|
|
'kota' => $kota,
|
|
'lat' => $lat,
|
|
'lng' => $lng,
|
|
]);
|