62 lines
1.9 KiB
PHP
62 lines
1.9 KiB
PHP
<?php
|
|
require_once('include/headscript.php');
|
|
|
|
/**
|
|
* Geocoding indirizzo -> coordinate. Proxy server-side verso OpenStreetMap /
|
|
* Nominatim (gratuito, senza API key). Ritorna JSON:
|
|
* { ok:true, lat:45.66, lng:8.79, display:"..." } oppure { ok:false, error:"..." }
|
|
*
|
|
* Nota: Nominatim chiede max 1 richiesta/secondo e uno User-Agent identificativo
|
|
* (impostato sotto). Quando avrai la chiave Google Maps si può passare alla
|
|
* Geocoding API di Google per maggiore precisione, cambiando solo questo file.
|
|
*/
|
|
if (function_exists('ob_get_level') && ob_get_level()) {
|
|
ob_clean();
|
|
}
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
$q = trim($_GET['q'] ?? '');
|
|
if ($q === '') {
|
|
echo json_encode(['ok' => false, 'error' => 'Indirizzo mancante.']);
|
|
exit();
|
|
}
|
|
|
|
$url = 'https://nominatim.openstreetmap.org/search?format=json&limit=1&addressdetails=0&q=' . rawurlencode($q);
|
|
$ua = 'YogiBook/1.0 (info@yogasoul.it)';
|
|
$json = false;
|
|
|
|
if (function_exists('curl_init')) {
|
|
$ch = curl_init($url);
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_USERAGENT => $ua,
|
|
CURLOPT_TIMEOUT => 8,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
]);
|
|
$json = curl_exec($ch);
|
|
curl_close($ch);
|
|
}
|
|
|
|
if ($json === false) {
|
|
$ctx = stream_context_create(['http' => [
|
|
'method' => 'GET',
|
|
'header' => "User-Agent: {$ua}\r\n",
|
|
'timeout' => 8,
|
|
]]);
|
|
$json = @file_get_contents($url, false, $ctx);
|
|
}
|
|
|
|
$data = $json ? json_decode($json, true) : null;
|
|
|
|
if (!empty($data[0]['lat']) && !empty($data[0]['lon'])) {
|
|
echo json_encode([
|
|
'ok' => true,
|
|
'lat' => round((float) $data[0]['lat'], 7),
|
|
'lng' => round((float) $data[0]['lon'], 7),
|
|
'display' => $data[0]['display_name'] ?? '',
|
|
]);
|
|
} else {
|
|
echo json_encode(['ok' => false, 'error' => 'Indirizzo non trovato. Prova a essere più specifico.']);
|
|
}
|
|
exit();
|