maps: search bias point is correct across the antimeridian
- Commit
19845c5f44d2d4956d465217225fc52e1b60c6f2- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 19:21 UTC (5 days ago)
- Parents
0ea04a1
When the map view crosses the antimeridian (bounds stored as west > east, e.g. 177 to -178 around Fiji) and the user is off-screen or has no fix, searchBiasPoint averaged the stored edges directly: (177 + -178)/2 = -0.5 โ off Ghana. Nearby-POI ranking then measured distances from ~19,000 km away. The user-inside test already unwrapped correctly; the fallback centre did not. centreLongitude now unwraps east by +360 before averaging and wraps the result past 180, the single owning layer every search caller passes through. New regression tests: the Fiji view centres on (179.5, -17.5) for an off-screen user and the no-fix sentinel, and a 170 -> -170 view stays at 180. Route geometry, navigation state, POI ranking, opening hours and link parsing were audited and cleared. Verified: :maps:testDevUnitTest passes; lint errors are pre-existing on untouched files.
Diff
maps/src/main/java/com/vayunmathur/maps/domain/CameraBounds.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/domain/CameraBounds.kt b/maps/src/main/java/com/vayunmathur/maps/domain/CameraBounds.ktindex 897c3b8bb6..9c994df6e0 100644--- a/maps/src/main/java/com/vayunmathur/maps/domain/CameraBounds.kt+++ b/maps/src/main/java/com/vayunmathur/maps/domain/CameraBounds.kt@@ -24,6 +24,17 @@ private val World = GeoBounds(west = -180.0, south = -85.0, east = 180.0, north fun CameraState.visibleBoundsOrWorld(): GeoBounds = projection?.queryVisibleBoundingBox() ?: World +/**+ * The longitude of the middle of [bounds]. A view straddling the antimeridian stores+ * west > east (177 to -178), whose plain average is -0.5 โ the opposite hemisphere โ so+ * the east edge is unwrapped past 180 before averaging and the result wrapped back.+ */+private fun centreLongitude(bounds: GeoBounds): Double {+ if (bounds.west <= bounds.east) return (bounds.west + bounds.east) / 2.0+ val centre = (bounds.west + bounds.east + 360.0) / 2.0+ return if (centre > 180.0) centre - 360.0 else centre+}+ /** * Where a search should be centred: the user's own position when they are looking at it, * otherwise the middle of the screen.@@ -45,6 +56,6 @@ fun searchBiasPoint(bounds: GeoBounds, user: GeoPoint): GeoPoint { return if (hasFix && lonInside && user.latitude in bounds.south..bounds.north) { user } else {- GeoPoint((bounds.east + bounds.west) / 2.0, (bounds.north + bounds.south) / 2.0)+ GeoPoint(centreLongitude(bounds), (bounds.north + bounds.south) / 2.0) } }
maps/src/test/java/com/vayunmathur/maps/util/CameraBoundsSlice2Test.kt
diff --git a/maps/src/test/java/com/vayunmathur/maps/util/CameraBoundsSlice2Test.kt b/maps/src/test/java/com/vayunmathur/maps/util/CameraBoundsSlice2Test.ktindex 9f981df27a..ef4035ba8d 100644--- a/maps/src/test/java/com/vayunmathur/maps/util/CameraBoundsSlice2Test.kt+++ b/maps/src/test/java/com/vayunmathur/maps/util/CameraBoundsSlice2Test.kt@@ -53,4 +53,20 @@ class CameraBoundsSlice2Test { val user = GeoPoint(179.5, -17.5) assertEquals(user, searchBiasPoint(fiji, user)) }++ @Test+ fun `a view across the antimeridian centres on the view, not the opposite hemisphere`() {+ val fiji = GeoBounds(west = 177.0, south = -19.0, east = -178.0, north = -16.0)+ // The view spans 177 eastward through 180 to -178 (5 degrees), so its+ // middle is 179.5. Averaging the stored edges naively gives -0.5.+ val centre = GeoPoint(179.5, -17.5)+ assertEquals(centre, searchBiasPoint(fiji, GeoPoint(2.3376, 48.8606)))+ assertEquals(centre, searchBiasPoint(fiji, GeoPoint(0.0, 0.0)))+ }++ @Test+ fun `a view centred exactly on the antimeridian stays there`() {+ val bounds = GeoBounds(west = 170.0, south = -19.0, east = -170.0, north = -16.0)+ assertEquals(180.0, searchBiasPoint(bounds, GeoPoint(0.0, 0.0)).longitude)+ } }