69 lines
2.1 KiB
PHP
69 lines
2.1 KiB
PHP
<?php
|
|
|
|
header("Content-Type: application/json");
|
|
|
|
$destinos_dir = "destinos"; // Carpeta principal
|
|
$destinos_array = [];
|
|
|
|
// OpenStreetMap solo permite uso con UserAgent. Indicamos que somos linuxeros...
|
|
function obtenerCoordenadas($nombre_destino) {
|
|
$url = "https://nominatim.openstreetmap.org/search?format=json&q=" . urlencode($nombre_destino);
|
|
|
|
// Iniciar sesión cURL
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64; rv:139.0) Gecko/20100101 Firefox/139.0");
|
|
|
|
// Ejecutar y obtener respuesta
|
|
$datos = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
// Decodificar JSON
|
|
$resultado = json_decode($datos, true);
|
|
|
|
if (!empty($resultado)) {
|
|
return [
|
|
"latitud" => $resultado[0]["lat"],
|
|
"longitud" => $resultado[0]["lon"]
|
|
];
|
|
} else {
|
|
return null; // No se encontraron coordenadas
|
|
}
|
|
}
|
|
|
|
|
|
// Recorrer todas las carpetas dentro de "destinos"
|
|
foreach (scandir($destinos_dir) as $carpeta) {
|
|
if ($carpeta === "." || $carpeta === "..") continue;
|
|
|
|
$json_path = "$destinos_dir/$carpeta/destino.json"; // Ruta del JSON dentro de cada carpeta
|
|
if (!file_exists($json_path)) continue;
|
|
|
|
// Leer datos del JSON
|
|
$json_data = file_get_contents($json_path);
|
|
$destino_info = json_decode($json_data, true);
|
|
|
|
// Obtener coordenadas si no existen
|
|
if (!isset($destino_info["Latitud"], $destino_info["Longitud"])) {
|
|
$coordenadas = obtenerCoordenadas($destino_info["Destino"]);
|
|
if ($coordenadas) {
|
|
$destino_info["Latitud"] = $coordenadas["latitud"];
|
|
$destino_info["Longitud"] = $coordenadas["longitud"];
|
|
}
|
|
}
|
|
|
|
// Agregar destino al array final
|
|
if (isset($destino_info["Latitud"], $destino_info["Longitud"])) {
|
|
$destinos_array[] = [
|
|
"nombre" => $destino_info["Destino"],
|
|
"lat" => $destino_info["Latitud"],
|
|
"lng" => $destino_info["Longitud"]
|
|
];
|
|
}
|
|
}
|
|
|
|
// Convertir datos a JSON y enviarlos al frontend
|
|
echo json_encode($destinos_array);
|
|
?>
|