@cloudnux/local-cloud-provider 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js
CHANGED
|
@@ -1587,6 +1587,82 @@ function createLocalLocationService() {
|
|
|
1587
1587
|
throw new Error(`Place not found: ${placeId}`);
|
|
1588
1588
|
}
|
|
1589
1589
|
return toLocationAddress(place);
|
|
1590
|
+
},
|
|
1591
|
+
/**
|
|
1592
|
+
* Calculate route between origin and destination with optional waypoints
|
|
1593
|
+
* Use case: User plans trip → Show route on map + details
|
|
1594
|
+
*/
|
|
1595
|
+
async calculateRoute(origin, destination, waypoints) {
|
|
1596
|
+
const allPoints = [origin, ...waypoints || [], destination];
|
|
1597
|
+
let totalDistanceKm = 0;
|
|
1598
|
+
const allCoordinates = [];
|
|
1599
|
+
const legs = [];
|
|
1600
|
+
for (let i = 0; i < allPoints.length - 1; i++) {
|
|
1601
|
+
const start = allPoints[i];
|
|
1602
|
+
const end = allPoints[i + 1];
|
|
1603
|
+
const legDistanceMeters = calculateDistance(start.lat, start.lng, end.lat, end.lng);
|
|
1604
|
+
const legDistanceKm = legDistanceMeters / 1e3;
|
|
1605
|
+
totalDistanceKm += legDistanceKm;
|
|
1606
|
+
const legCoordinates = [];
|
|
1607
|
+
const numIntermediatePoints = 10;
|
|
1608
|
+
for (let j = 0; j <= numIntermediatePoints; j++) {
|
|
1609
|
+
const fraction = j / numIntermediatePoints;
|
|
1610
|
+
legCoordinates.push({
|
|
1611
|
+
lat: start.lat + (end.lat - start.lat) * fraction,
|
|
1612
|
+
lng: start.lng + (end.lng - start.lng) * fraction
|
|
1613
|
+
});
|
|
1614
|
+
}
|
|
1615
|
+
const legDurationMinutes = legDistanceKm / 50 * 60;
|
|
1616
|
+
legs.push({
|
|
1617
|
+
distanceKm: legDistanceKm,
|
|
1618
|
+
durationMinutes: legDurationMinutes,
|
|
1619
|
+
coordinates: legCoordinates,
|
|
1620
|
+
startPosition: start,
|
|
1621
|
+
endPosition: end
|
|
1622
|
+
});
|
|
1623
|
+
if (i === 0) {
|
|
1624
|
+
allCoordinates.push(...legCoordinates);
|
|
1625
|
+
} else {
|
|
1626
|
+
allCoordinates.push(...legCoordinates.slice(1));
|
|
1627
|
+
}
|
|
1628
|
+
}
|
|
1629
|
+
const totalDurationMinutes = totalDistanceKm / 50 * 60;
|
|
1630
|
+
const sampledPoints = [];
|
|
1631
|
+
const sampleInterval = Math.max(1, Math.floor(allCoordinates.length / 20));
|
|
1632
|
+
let accumulatedDistance = 0;
|
|
1633
|
+
for (let i = 0; i < allCoordinates.length; i += sampleInterval) {
|
|
1634
|
+
if (i > 0) {
|
|
1635
|
+
const prev = allCoordinates[i - sampleInterval] || allCoordinates[i - 1];
|
|
1636
|
+
const curr = allCoordinates[i];
|
|
1637
|
+
accumulatedDistance += calculateDistance(prev.lat, prev.lng, curr.lat, curr.lng) / 1e3;
|
|
1638
|
+
}
|
|
1639
|
+
sampledPoints.push({
|
|
1640
|
+
lat: allCoordinates[i].lat,
|
|
1641
|
+
lng: allCoordinates[i].lng,
|
|
1642
|
+
distanceFromStartKm: accumulatedDistance
|
|
1643
|
+
});
|
|
1644
|
+
}
|
|
1645
|
+
const lats = allCoordinates.map((c) => c.lat);
|
|
1646
|
+
const lngs = allCoordinates.map((c) => c.lng);
|
|
1647
|
+
const bbox = [
|
|
1648
|
+
Math.min(...lngs),
|
|
1649
|
+
// minLng
|
|
1650
|
+
Math.min(...lats),
|
|
1651
|
+
// minLat
|
|
1652
|
+
Math.max(...lngs),
|
|
1653
|
+
// maxLng
|
|
1654
|
+
Math.max(...lats)
|
|
1655
|
+
// maxLat
|
|
1656
|
+
];
|
|
1657
|
+
logger.info(`Calculated route: ${totalDistanceKm.toFixed(2)}km, ${totalDurationMinutes.toFixed(0)}min, ${legs.length} legs`);
|
|
1658
|
+
return {
|
|
1659
|
+
totalDistanceKm,
|
|
1660
|
+
totalDurationMinutes,
|
|
1661
|
+
coordinates: allCoordinates,
|
|
1662
|
+
sampledPoints,
|
|
1663
|
+
bbox,
|
|
1664
|
+
legs
|
|
1665
|
+
};
|
|
1590
1666
|
}
|
|
1591
1667
|
};
|
|
1592
1668
|
}
|