The existing calculated route, a Feature with LineString geometry.
The existing waypoints in order, from origin through intermediate waypoints to destination.
The insertion index. 0 inserts at the beginning, and existingWaypoints.length appends at the end.
// A route from [4.9, 52.3] to [5.0, 52.4], through [4.95, 52.35]
const waypoints: WaypointLike[] = [[4.9, 52.3], [5.0, 52.4]];
const newWaypoint: WaypointLike = [4.95, 52.35];
const insertIndex = findBestWaypointInsertionIndex(route, waypoints, newWaypoint);
// Returns: 1 (insert between origin and destination)
const updatedWaypoints = [
...waypoints.slice(0, insertIndex),
newWaypoint,
...waypoints.slice(insertIndex)
];
Finds the best index to insert a new waypoint into an existing route.
It picks the position where the new waypoint fits most naturally along the route line: