feat: optimización de ETA, limpieza automática de rutas y smart location
This commit is contained in:
@ -59,24 +59,14 @@ export function useParadaCercana() {
|
||||
const { RoutesService } = await google.maps.importLibrary("routes") as any;
|
||||
const routeService = new RoutesService();
|
||||
|
||||
for (const stop of top5) {
|
||||
const routePromises = top5.map(async (stop) => {
|
||||
try {
|
||||
const response = await routeService.computeRoutes({
|
||||
origin: {
|
||||
location: {
|
||||
latLng: {
|
||||
latitude: ubicacionUsuario.lat,
|
||||
longitude: ubicacionUsuario.lng
|
||||
}
|
||||
}
|
||||
location: { latLng: { latitude: ubicacionUsuario.lat, longitude: ubicacionUsuario.lng } }
|
||||
},
|
||||
destination: {
|
||||
location: {
|
||||
latLng: {
|
||||
latitude: stop.latitude,
|
||||
longitude: stop.longitude
|
||||
}
|
||||
}
|
||||
location: { latLng: { latitude: stop.latitude, longitude: stop.longitude } }
|
||||
},
|
||||
travelMode: 'DRIVE',
|
||||
routingPreference: 'TRAFFIC_UNAWARE',
|
||||
@ -86,31 +76,28 @@ export function useParadaCercana() {
|
||||
|
||||
if (response.routes && response.routes.length > 0) {
|
||||
const route = response.routes[0];
|
||||
let distTotal = 0;
|
||||
let durTotal = 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);
|
||||
}
|
||||
}
|
||||
return {
|
||||
stop,
|
||||
distance: route.distanceMeters || Infinity,
|
||||
duration: parseInt(route.duration || "0"),
|
||||
points: route.polyline?.encodedPolyline ? google.maps.geometry.encoding.decodePath(route.polyline.encodedPolyline) : []
|
||||
};
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('Error calculando ruta a parada', stop.name, e);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
const results = await Promise.all(routePromises);
|
||||
|
||||
for (const res of results) {
|
||||
if (res && res.distance < minimaDistanciaCalles) {
|
||||
minimaDistanciaCalles = res.distance;
|
||||
mejorDuracion = res.duration;
|
||||
mejorParada = res.stop;
|
||||
mejorRutaPuntos = res.points;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error cargando Routes API en useParadaCercana', e);
|
||||
|
||||
Reference in New Issue
Block a user