Skip to content

RTFDB-4842: Improve user input handling in "mapbox-gl-geocoder" - #559

Open
pahuta wants to merge 2 commits into
RTFDB-4842-improve-user-input-handling-3from
RTFDB-4842-improve-user-input-handling-4
Open

RTFDB-4842: Improve user input handling in "mapbox-gl-geocoder"#559
pahuta wants to merge 2 commits into
RTFDB-4842-improve-user-input-handling-3from
RTFDB-4842-improve-user-input-handling-4

Conversation

@pahuta

@pahuta pahuta commented Aug 5, 2026

Copy link
Copy Markdown
  • Add function to parse different spatial formats which can be used to configure localGeocoder function
  • Add isValidTile, isValidQuadkey, tileToLngLat and quadkeyToTile helpers to lib/utils.js
  • Use the parsed zoom when flying to a selected feature that has no bbox
  • Fix the suggestion list not updating after cutting (Cmd/Ctrl+X) or undoing (Cmd/Ctrl+Z) a change to the search input

https://mapbox.atlassian.net/browse/RTFDB-4842

  • briefly describe the changes in this PR
  • write tests for all new functionality
  • run npm run docs and commit changes to API.md
  • update CHANGELOG.md with changes under main heading before merging

Parsing extended spatial formats when enabled:

parseExtendedSpatialFormats.mp4

Previous vs current behavior when user cut or undo search input value

cut_undo.mp4

- Add optional `parseExtendedSpatialFormats` option with four independently
  toggled formats, all disabled by default: `commaSeparatedLngLatZoom`
  (`lng,lat,zoom`), `slashSeparatedZoomLatLng` (`zoom/lat/lng`),
  `tile` (`z/x/y`) and `quadkey`
- Add `lib/spatial-formats.js`, which turns a matching search input into a
  synthetic GeoJSON feature and prepends it to the suggestion list on both
  the successful and the failed request path
- Add `isValidTile`, `isValidQuadkey`, `tileToLngLat` and `quadkeyToTile`
  helpers to `lib/utils.js`
- Use the parsed zoom when flying to a selected feature that has no `bbox`
- Fix `getSelectedIndex` in `lib/events.js` reporting index 0 for every
  result feature without an `id`

https://mapbox.atlassian.net/browse/RTFDB-4842
@pahuta
pahuta marked this pull request as ready for review August 5, 2026 09:42
@pahuta
pahuta requested a review from a team as a code owner August 5, 2026 09:42
@pahuta
pahuta requested review from underoot and removed request for a team August 5, 2026 09:42
Comment thread debug/index.js Outdated
trackProximity: true,
useBrowserFocus: true,
enableGeolocation: true,
parseExtendedSpatialFormats: {

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.

Why do we need to expose this as a public API?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

To give our users (including internal ones) the ability to quickly navigate to a desired location. It's practically the same as how coordinate search works now.

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.

So you expect end users to configure the accepted formats in UI?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

No, not on UI. Let's clarify, in this context "end users" are developers and they will just enable parsing predefined formats. Here is an example how we configure mapbox-gl-geocoder right now:

const geocoder = new MapboxGeocoder({
      accessToken,
      origin: config.MAPBOX_API_URL,
      useBrowserFocus: true,
      mapboxgl: mapboxgl,
      marker: false,
      reverseGeocode: true,
      flipCoordinates: true
    });

These are just a few additional configurations which are fully backwards compatible with a current default behavior. Example how new parseExtendedSpatialFormats will be used:

const geocoder = new MapboxGeocoder({
      accessToken,
      origin: config.MAPBOX_API_URL,
      useBrowserFocus: true,
      mapboxgl: mapboxgl,
      marker: false,
      reverseGeocode: true,
      flipCoordinates: true,
      // new config below
      parseExtendedSpatialFormats: {
        commaSeparatedLngLatZoom: true,
        slashSeparatedZoomLatLng: true,
        tile: true,
        quadkey: true
      }
    });

After that users on UI will be able to specify coordinates/tile/quadkey in the search input and navigate to the location.

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.

I still don't understand why this needs to be configurable. Do you expect developers to explicitly opt out of some formats? Also, why did you choose these formats (i.e., why lnglat and not latlng)? I wonder if we could enable this by default, improve the detection logic, and avoid introducing such a public API at all.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Changed the implementation as we discussed: indeed localGeocoder covers our needs and there is no sense to introduce the parseExtendedSpatialFormats parameter.

Comment thread lib/spatial-formats.js Outdated

// Marks a feature synthesized from an extended spatial format, so it can be told
// apart from Geocoding API results (which carry `_source: 'mapbox'`).
const SOURCE = 'extended-spatial-format';

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.

Why do we need syntetic features at all?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The same reason as in previous comment. I agree that it's not directly related to the geocoding but it significantly improve UX. For example in our user facing applications in Mapbox Console user will be able to quickly move the map to the desired location if he/she has coordinates in other format than apps support.

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.

The feature itself is good, but I wonder if we could simplify the implementation

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I open to the suggestion. In general code of mapbox-gl-geocoder is old and written in "all in one file" style. I tried to keep it maintainable by putting new functionality separately as much as possible and maintaining backward compatibility.

- remove a `parseExtendedSpatialFormats` contractor parameter in favor of using a `localGeocoder`

https://mapbox.atlassian.net/browse/RTFDB-4842
@pahuta
pahuta requested a review from stepankuzmin August 10, 2026 15:14
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.

2 participants