Skip to content

fix: fill first empty waypoint slot instead of counting filled waypoints#480

Open
wursterje wants to merge 2 commits into
Project-OSRM:gh-pagesfrom
wursterje:gh-pages
Open

fix: fill first empty waypoint slot instead of counting filled waypoints#480
wursterje wants to merge 2 commits into
Project-OSRM:gh-pagesfrom
wursterje:gh-pages

Conversation

@wursterje

Copy link
Copy Markdown

Problem

When the page is opened with only the destination pre-filled via URL (?loc=&loc=<dest>), clicking the map overwrites the destination instead of filling the empty start slot.

The waypoint array looks like this:

Index latLng
0 null
1 {...}

addWaypoint() counted the number of filled waypoints (= 1) and derived the target slot from that count. Since it assumed filled waypoints are always packed at the front, it computed slot 1 and overwrote the destination.

Fix

Instead of counting filled waypoints, scan the array for the first slot without a coordinate and fill that. If all slots are filled, fall back to replacing the last one (previous behaviour).

Use case

Sites that link to the router with a pre-filled destination (?loc=&loc=<institution>) so visitors can set their own start point
with a single map click. The old behaviour destroyed the destination on that click.

Fixes #479

@DennisOSRM

Copy link
Copy Markdown
Contributor

Thanks so much for the contribution. Will start the review sometime this week.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes map-click waypoint placement when the route is opened with an empty start and a pre-filled destination (e.g. ?loc=&loc=<dest>), by selecting the first empty waypoint slot instead of deriving the target slot from the count of filled waypoints.

Changes:

  • Store lrmControl.getWaypoints() in a local waypoints variable for reuse.
  • Replace the “count-filled-waypoints → compute index” logic with “find first empty slot → fill it”.
  • Keep existing behavior when all slots are filled by replacing the last waypoint.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/index.js
Comment on lines 716 to 717
return;
}
Comment thread src/index.js
Comment on lines +734 to +735
// All slots are filled: replace the last one.
lrmControl.spliceWaypoints(waypoints.length - 1, 1, waypoint);
@DennisOSRM

DennisOSRM commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

@wursterje Please have a look at the comments from the review bot, especially the second comment on using max(.) to make sure the index doesn't become negative. Otherwise, this is looking good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Map click overwrites pre-filled destination when start waypoint is empty

3 participants