Skip to content

fix: preserve exact coordinates entered in waypoint input fields#392

Merged
DennisOSRM merged 3 commits intogh-pagesfrom
fix/preserve-coordinate-input
Mar 28, 2026
Merged

fix: preserve exact coordinates entered in waypoint input fields#392
DennisOSRM merged 3 commits intogh-pagesfrom
fix/preserve-coordinate-input

Conversation

@DennisOSRM
Copy link
Copy Markdown
Contributor

@DennisOSRM DennisOSRM commented Mar 27, 2026

Problem

When a user types raw GPS coordinates (e.g. 34.129382,-118.141254) into a waypoint input field, Nominatim forward-geocodes the string and snaps the result to the nearest named address — sometimes more than a block away from the entered point.

Entering the same coordinates directly in the URL (?loc=34.129382,-118.141254) works correctly because that path bypasses geocoding.

Root Cause

LRM's autocomplete fires a suggest call as the user types. The suggest results (from Nominatim forward-geocode) appear in a dropdown and get auto-selected. When Enter is pressed, LRM uses that pre-selected suggest result — with Nominatim's snapped center — instead of the exact coordinates.

Fix

Introduces a coordinate-preserving geocoder wrapper (geocoder.coordPreserving()) that detects coordinate strings in both the geocode and suggest paths:

  • Coordinate input: Nominatim reverse-geocodes the point for a human-readable display name, but the result's center is overridden with the exact typed lat/lon.
  • Address input: Nominatim forward-geocode is used as before (no change in behavior).
  • The wrapper also bridges leaflet-control-geocoder's Promise-based API to the callback-based API that leaflet-routing-machine's autocomplete expects.

Files Changed

  • src/geocoder.js — Added coordPreserving() factory with coordinate detection and Nominatim reverse-geocode
  • src/index.js — Use coordPreserving() geocoder; restore leaflet-control-geocoder side-effect import
  • bundle.js, bundle.js.map, bundle.raw.js — Rebuilt

Fixes #154

gpsroute

When a user types raw GPS coordinates (e.g. '34.129382,-118.141254')
into a waypoint field, the previous behaviour sent them to Nominatim
as a forward-geocode query, which snapped the result to the nearest
named address — sometimes more than a block away from the typed point.

Root cause: LRM's autocomplete fires a 'suggest' call as the user
types. The suggest results appear in a dropdown and get auto-selected.
When Enter is pressed, LRM uses that pre-selected suggest result
(with the Nominatim-snapped center) instead of calling geocode.

Fix: introduce a coord-preserving geocoder wrapper that detects
coordinate strings in both the geocode and suggest paths:

- If the input is coordinates, Nominatim reverse-geocodes the point
  for a human-readable display name, but the result's center is
  overridden with the exact typed lat/lon.
- If the input is a plain address, Nominatim forward-geocode is used
  as before.
- The wrapper bridges the library's Promise-based API to the
  callback-based API that leaflet-routing-machine expects.

Fixes #154

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 27, 2026 15:57
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a coordinate-preserving geocoding path for waypoint input fields so that raw lat/lon inputs keep the exact typed coordinates (while still showing a reverse-geocoded display name), aligning typed-input behavior with the existing ?loc=lat,lon URL path.

Changes:

  • Introduced coordPreserving() geocoder wrapper that detects coordinate strings and reverse-geocodes for display while overriding the result center to the exact input.
  • Switched the routing plan’s geocoder to createGeocoder.coordPreserving() and adjusted the leaflet-control-geocoder import to be side-effect only.
  • Rebuilt distributable bundles (bundle.js, bundle.js.map, bundle.raw.js).

Reviewed changes

Copilot reviewed 2 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/geocoder.js Adds coordinate parsing + a wrapper geocoder that preserves exact centers and bridges Promise results to LRM’s callback autocomplete API.
src/index.js Uses the new coordinate-preserving geocoder for waypoint inputs; restores geocoder side-effect import.
bundle.js Rebuilt compiled bundle reflecting the new geocoder behavior.
bundle.js.map Updated sourcemap for rebuilt bundle.
bundle.raw.js Rebuilt raw bundle including updated sources/dependencies.

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

Comment thread src/geocoder.js
Comment thread src/geocoder.js Outdated

// Helper: reverse-geocodes coordinates for display name, but preserves exact latlng.
function coordResult(latlng, query) {
return nominatim.reverse(latlng, 67108864).then(function(results) {
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

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

67108864 is a scale constant corresponding to zoom 18 (256 * 2^18). Adding an inline comment (or deriving it from L.CRS.EPSG3857.scale(18)) would make the intent clearer and avoid this looking like an arbitrary magic number.

Suggested change
return nominatim.reverse(latlng, 67108864).then(function(results) {
// Use scale corresponding to zoom level 18 (was hard-coded as 256 * 2^18 = 67108864)
return nominatim.reverse(latlng, L.CRS.EPSG3857.scale(18)).then(function(results) {

Copilot uses AI. Check for mistakes.
@DennisOSRM DennisOSRM enabled auto-merge (squash) March 28, 2026 09:00
@DennisOSRM DennisOSRM disabled auto-merge March 28, 2026 09:01
@DennisOSRM DennisOSRM merged commit f7abbb6 into gh-pages Mar 28, 2026
@DennisOSRM DennisOSRM deleted the fix/preserve-coordinate-input branch March 28, 2026 09:02
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.

Routing Between GPS coordinates

2 participants