chore: migrate Google Maps to Routes API and clean up deprecated code
This commit is contained in:
@ -49,45 +49,70 @@ export function useParadaCercana() {
|
||||
paradasConDistLineal.sort((a, b) => a.distancia - b.distancia);
|
||||
const top5 = paradasConDistLineal.slice(0, 5).map(item => item.parada);
|
||||
|
||||
// 2. Usar Directions API para encontrar la más cercana por calles reales
|
||||
// 2. Usar Routes API para encontrar la más cercana por calles reales
|
||||
let mejorParada: BusStop | null = null;
|
||||
let minimaDistanciaCalles = Infinity;
|
||||
let mejorDuracion = 0;
|
||||
let mejorRutaPuntos: google.maps.LatLng[] = [];
|
||||
|
||||
const directionsService = new google.maps.DirectionsService();
|
||||
try {
|
||||
const { Route } = await google.maps.importLibrary("routes") as any;
|
||||
|
||||
for (const stop of top5) {
|
||||
try {
|
||||
const response = await directionsService.route({
|
||||
origin: new google.maps.LatLng(ubicacionUsuario.lat, ubicacionUsuario.lng),
|
||||
destination: new google.maps.LatLng(stop.latitude, stop.longitude),
|
||||
travelMode: google.maps.TravelMode.DRIVING // Calles reales
|
||||
});
|
||||
for (const stop of top5) {
|
||||
try {
|
||||
const response = await Route.computeRoutes({
|
||||
origin: {
|
||||
location: {
|
||||
latLng: {
|
||||
latitude: ubicacionUsuario.lat,
|
||||
longitude: ubicacionUsuario.lng
|
||||
}
|
||||
}
|
||||
},
|
||||
destination: {
|
||||
location: {
|
||||
latLng: {
|
||||
latitude: stop.latitude,
|
||||
longitude: stop.longitude
|
||||
}
|
||||
}
|
||||
},
|
||||
travelMode: 'DRIVE',
|
||||
routingPreference: 'TRAFFIC_UNAWARE',
|
||||
polylineQuality: 'HIGH_QUALITY',
|
||||
polylineEncoding: 'ENCODED_POLYLINE',
|
||||
});
|
||||
|
||||
if (response.routes && response.routes.length > 0) {
|
||||
const route = response.routes[0];
|
||||
if (!route) continue;
|
||||
let distTotal = 0;
|
||||
let durTotal = 0;
|
||||
if (response.routes && response.routes.length > 0) {
|
||||
const route = response.routes[0];
|
||||
let distTotal = 0;
|
||||
let durTotal = 0;
|
||||
|
||||
if (route.legs) {
|
||||
for (const leg of route.legs) {
|
||||
distTotal += leg.distance?.value || 0;
|
||||
durTotal += leg.duration?.value || 0;
|
||||
if (route.distanceMeters) {
|
||||
distTotal = route.distanceMeters;
|
||||
}
|
||||
|
||||
if (route.duration) {
|
||||
// La duración viene como string "123s"
|
||||
durTotal = parseInt(route.duration);
|
||||
}
|
||||
|
||||
if (distTotal < minimaDistanciaCalles) {
|
||||
minimaDistanciaCalles = distTotal;
|
||||
mejorDuracion = durTotal;
|
||||
mejorParada = stop;
|
||||
|
||||
if (route.polyline && route.polyline.encodedPolyline) {
|
||||
mejorRutaPuntos = google.maps.geometry.encoding.decodePath(route.polyline.encodedPolyline);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (distTotal < minimaDistanciaCalles) {
|
||||
minimaDistanciaCalles = distTotal;
|
||||
mejorDuracion = durTotal;
|
||||
mejorParada = stop;
|
||||
mejorRutaPuntos = route.overview_path;
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('Error calculando ruta a parada', stop.name, e);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('Error calculando ruta a parada', stop.name, e);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error cargando Routes API en useParadaCercana', e);
|
||||
}
|
||||
|
||||
// 3. Fallback a la más cercana lineal si falla API
|
||||
|
||||
Reference in New Issue
Block a user