maps: rank search from the user's position when it is on screen (#mapsearch)
- Commit
aebed7368e243e86c74234b29f0f02d399a076b5- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 20:32 UTC (22 days ago)
- Parents
85e56c7
Symptom (owner): results "super far away instead of right here". Search was always biased to the centre of the visible map. With the map nudged a few km off the user, or zoomed out to the whole metro, "pizza" was ranked from wherever the middle of the screen happened to be rather than from the person holding the phone — and since the ranking is now strongly distance-weighted (previous commit), the bias point matters more than it did. Change: a pure `searchBiasPoint(bounds, user)` in util/CameraBounds.kt picks the user's fix when it lies inside the visible bounds (handles a view across the antimeridian), otherwise the screen centre as before. A user who pans to another city and searches there still means that city; no fix yet ((0, 0), as elsewhere in the map) falls back to the centre. MapSearchHost.openSearch (search sheet, category chips, voice) and the contact-address shortcut in MapSearchBarSection both use it. MapSearchHost takes the SelectedFeatureViewModel's userPosition flow (defaulting to "no fix" so previews/tests construct it unchanged); MapPage stays at 346 lines (ui FileLength limit 350). Tests: CameraBoundsSlice2Test covers user-on-screen, user elsewhere / no fix, and the antimeridian case.
Diff
maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt b/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.ktindex ed0689e5c3..effb5c7182 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt@@ -217,7 +217,7 @@ fun MapPage( // How much of the bottom the collapsed search bar is occupying, so the FAB stack and the // scale bar can clear it the same way they clear a sheet. Zero whenever the bar is not drawn. var searchBarLiftPx by remember { mutableIntStateOf(0) }- val searchHost = rememberMapSearchHost(searchViewModel, camera)+ val searchHost = rememberMapSearchHost(searchViewModel, camera, viewModel.userPosition) val searchRequest = searchHost.searchRequest val searchState = SearchUiState( query = searchQuery,
maps/src/main/java/com/vayunmathur/maps/ui/MapSearchBarSection.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/MapSearchBarSection.kt b/maps/src/main/java/com/vayunmathur/maps/ui/MapSearchBarSection.ktindex 59209880e2..0e18971144 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/MapSearchBarSection.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/MapSearchBarSection.kt@@ -6,6 +6,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.layout.onSizeChanged import com.vayunmathur.maps.ui.map.MapSearchBar import com.vayunmathur.maps.ui.theme.MapChromeMetrics+import com.vayunmathur.maps.util.searchBiasPoint import com.vayunmathur.maps.util.visibleBoundsOrWorld /**@@ -27,12 +28,8 @@ internal fun MapPageScope.MapSearchBarSection( MapSearchBar( onOpenSearch = { query -> openSearch(query, null) }, onContactAddress = { address ->- val bbox = camera.visibleBoundsOrWorld()- searchViewModel.resolveAndSelect(- address,- (bbox.north + bbox.south) / 2.0,- (bbox.east + bbox.west) / 2.0,- ) { place ->+ val near = searchBiasPoint(camera.visibleBoundsOrWorld(), userPosition)+ searchViewModel.resolveAndSelect(address, near.latitude, near.longitude) { place -> if (place != null) { viewModel.stashRouteSelection() // Fly there and open the peek pane, the same direct-open
maps/src/main/java/com/vayunmathur/maps/ui/MapSheetEffects.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/MapSheetEffects.kt b/maps/src/main/java/com/vayunmathur/maps/ui/MapSheetEffects.ktindex f67fca456d..4db8e28ff4 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/MapSheetEffects.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/MapSheetEffects.kt@@ -10,6 +10,7 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.res.stringResource import com.vayunmathur.library.map.CameraState+import com.vayunmathur.library.map.GeoPoint import com.vayunmathur.library.ui.FreeHeightSheetState import com.vayunmathur.library.ui.IconSettings import com.vayunmathur.library.ui.OverlayAction@@ -19,7 +20,10 @@ import com.vayunmathur.maps.Route import com.vayunmathur.maps.data.SpecificFeature import com.vayunmathur.maps.util.MapsSearchViewModel import com.vayunmathur.maps.util.SelectedFeatureViewModel+import com.vayunmathur.maps.util.searchBiasPoint import com.vayunmathur.maps.util.visibleBoundsOrWorld+import kotlinx.coroutines.flow.MutableStateFlow+import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch /**@@ -30,6 +34,7 @@ import kotlinx.coroutines.launch internal class MapSearchHost( val searchViewModel: MapsSearchViewModel, val camera: CameraState,+ private val userPosition: StateFlow<GeoPoint> = NoFix, ) { var searchRequest by mutableStateOf<SearchRequest?>(null) @@ -39,9 +44,9 @@ internal class MapSearchHost( var searchWasOpen by mutableStateOf(false) fun openSearch(query: String? = null, waypointIndex: Int? = null) {- val bbox = camera.visibleBoundsOrWorld()- val nearLat = (bbox.north + bbox.south) / 2.0- val nearLon = (bbox.east + bbox.west) / 2.0+ val near = searchBiasPoint(camera.visibleBoundsOrWorld(), userPosition.value)+ val nearLat = near.latitude+ val nearLon = near.longitude // The category chips and the voice transcript both arrive as a query to run, which used // to be a route argument the destination replayed on arrival. There is no arrival any // more, so it runs here.@@ -50,13 +55,17 @@ internal class MapSearchHost( } } +/** "No fix yet" for hosts built without a location source (previews, tests). */+private val NoFix: StateFlow<GeoPoint> = MutableStateFlow(GeoPoint(0.0, 0.0))+ /** Remembers a [MapSearchHost] bound to this composition. */ @Composable internal fun rememberMapSearchHost( searchViewModel: MapsSearchViewModel, camera: CameraState,-): MapSearchHost = remember(searchViewModel, camera) {- MapSearchHost(searchViewModel, camera)+ userPosition: StateFlow<GeoPoint> = NoFix,+): MapSearchHost = remember(searchViewModel, camera, userPosition) {+ MapSearchHost(searchViewModel, camera, userPosition) } /**
maps/src/main/java/com/vayunmathur/maps/util/CameraBounds.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/util/CameraBounds.kt b/maps/src/main/java/com/vayunmathur/maps/util/CameraBounds.ktindex e3bece6661..73a8980ae4 100644--- a/maps/src/main/java/com/vayunmathur/maps/util/CameraBounds.kt+++ b/maps/src/main/java/com/vayunmathur/maps/util/CameraBounds.kt@@ -2,6 +2,7 @@ package com.vayunmathur.maps.util import com.vayunmathur.library.map.CameraState import com.vayunmathur.library.map.GeoBounds+import com.vayunmathur.library.map.GeoPoint /** * The world, used when the camera cannot say what is on screen.@@ -22,3 +23,28 @@ private val World = GeoBounds(west = -180.0, south = -85.0, east = 180.0, north */ fun CameraState.visibleBoundsOrWorld(): GeoBounds = projection?.queryVisibleBoundingBox() ?: World++/**+ * Where a search should be centred: the user's own position when they are looking at it,+ * otherwise the middle of the screen.+ *+ * Search used to rank from the screen centre only, so with the map nudged a few km away+ * (or zoomed out to the metro) "pizza" was ranked from wherever the middle of the screen+ * happened to be rather than from the person holding the phone. The fix only wins while it+ * is on screen: a user who pans to another city and searches there means that city. No fix+ * yet is (0, 0) — the same "unset" the rest of the map uses.+ */+fun searchBiasPoint(bounds: GeoBounds, user: GeoPoint): GeoPoint {+ val hasFix = user.latitude != 0.0 || user.longitude != 0.0+ val lonInside = if (bounds.west <= bounds.east) {+ user.longitude in bounds.west..bounds.east+ } else {+ // The view straddles the antimeridian.+ user.longitude >= bounds.west || user.longitude <= bounds.east+ }+ 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)+ }+}
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 59bec13536..36ef08762c 100644--- a/maps/src/test/java/com/vayunmathur/maps/util/CameraBoundsSlice2Test.kt+++ b/maps/src/test/java/com/vayunmathur/maps/util/CameraBoundsSlice2Test.kt@@ -1,6 +1,7 @@ package com.vayunmathur.maps.util import com.vayunmathur.library.map.GeoBounds+import com.vayunmathur.library.map.GeoPoint import kotlin.test.Test import kotlin.test.assertEquals @@ -28,4 +29,26 @@ class CameraBoundsSlice2Test { val b = com.vayunmathur.library.map.CameraState().visibleBoundsOrWorld() assertEquals(a, b) }++ private val sf = GeoBounds(west = -122.45, south = 37.76, east = -122.38, north = 37.81)++ @Test+ fun `search centres on the user when they are on screen`() {+ val user = GeoPoint(-122.4075, 37.7879)+ assertEquals(user, searchBiasPoint(sf, user))+ }++ @Test+ fun `search centres on the screen when the user is elsewhere or has no fix`() {+ val centre = GeoPoint((-122.45 + -122.38) / 2.0, (37.76 + 37.81) / 2.0)+ assertEquals(centre, searchBiasPoint(sf, GeoPoint(2.3376, 48.8606)))+ assertEquals(centre, searchBiasPoint(sf, GeoPoint(0.0, 0.0)))+ }++ @Test+ fun `a view across the antimeridian still sees the user`() {+ val fiji = GeoBounds(west = 177.0, south = -19.0, east = -178.0, north = -16.0)+ val user = GeoPoint(179.5, -17.5)+ assertEquals(user, searchBiasPoint(fiji, user))+ } }