This is the documentation of version 1.0.0-rc.3. Go to the latest version, 1.0.0-rc.5.
TomTom Maps for JavaScript
    Preparing search index...

    Function findBestWaypointInsertionIndex

    • 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:

      1. It projects each existing waypoint onto its closest point on the route line.
      2. It projects the new waypoint onto the route line.
      3. It places the new waypoint by its position along the route.

      Parameters

      • route: Route

        The existing calculated route, a Feature with LineString geometry.

      • existingWaypoints: WaypointLike[]

        The existing waypoints in order, from origin through intermediate waypoints to destination.

      • newWaypoint: WaypointLike

      Returns number

      The insertion index. 0 inserts at the beginning, and existingWaypoints.length appends at the end.

      • If the route has fewer than 2 coordinates or there are fewer than 2 existing waypoints, it returns 0.
      • Fitting the waypoint along the route's progression minimizes detours, which keeps the route streamlined.
      const route: Route = {
      type: 'Feature',
      id: 'route-1',
      geometry: {
      type: 'LineString',
      coordinates: [[4.9, 52.3], [4.95, 52.35], [5.0, 52.4]]
      },
      properties: { ... },
      bbox: [4.9, 52.3, 5.0, 52.4]
      };

      const waypoints: WaypointLike[] = [
      [4.9, 52.3], // origin
      [5.0, 52.4] // destination
      ];

      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)
      ];