Skip to content

Fix distance calculation for nearby and antipodal points - #13749

Open
DestinyOfLove wants to merge 1 commit into
mapbox:mainfrom
DestinyOfLove:feat/fix-short-distance
Open

DestinyOfLove wants to merge 1 commit into
mapbox:mainfrom
DestinyOfLove:feat/fix-short-distance

Conversation

@DestinyOfLove

Copy link
Copy Markdown

Summary

LngLat.distanceTo() can return zero for distinct nearby coordinates, a positive distance for identical coordinates, or NaN for antipodal coordinates because the spherical law of cosines loses precision near its domain limits. For example, [5.1, 52.1] to [5.100001, 52.1] currently returns zero instead of approximately 0.06830549 m.

Use the Haversine formula described in the existing API documentation, retaining the existing Earth radius. Clamp its intermediate value to avoid floating-point overshoot at antipodal points.

Evidence

  • The three added regression cases fail before the change in Chromium and pass after it.
  • npm run test-unit -- test/unit/geo/lng_lat.test.ts
  • npm run tsc
  • npm run lint

Merge Danger

Distances may differ slightly due to floating-point rounding; this fixes larger errors for very short and antipodal distances. No public API changes or dependencies are introduced.

Launch Checklist

  • Make sure the PR title is descriptive and preferably reflects the change from the user's perspective.
  • Add additional detail and context in the PR description.
  • Manually test the debug page.
  • Write tests for all new functionality and make sure the CI checks pass.
  • Document any changes to public APIs (no API signature changes).
  • Post benchmark scores if the change could affect performance.

@DestinyOfLove
DestinyOfLove requested a review from a team as a code owner November 2, 2026 10:02
@DestinyOfLove
DestinyOfLove requested review from mourner and removed request for a team November 2, 2026 10:02
@CLAassistant

CLAassistant commented Nov 2, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Hey, @DestinyOfLove 👋 Thanks for your contribution to Mapbox GL JS!

Important: This repository does not accept direct merges. All changes go through our internal review process.

What happens next:

  1. A team member will review your PR here first
  2. If it looks good, they will import it to our internal repository for further review
  3. If approved, changes will be synced back here via our release process

Please respond to any review comments on this PR. For more details, see CONTRIBUTING.md.

This branch has not been deployed

No deployments
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