Modern Apps git

← Modern-Apps.git

maps: render ambient POI pins from the offline index via the proven GooglePoiLayer (replace failing ma_pois PMTiles overlay)

Commit
196bbae879ad17d31c89de54ab39eb5d94596440
Author
vayun-mathur <[email protected]>
When
2026-08-18 05:09 UTC (3 months ago)
Parents
727949e
- PoiIndex.inViewport(west,south,east,north,cap): bbox linear scan (cap ~300) returning decoded name+coord+type records for the visible viewport.
- GooglePoiMapViewModel.onViewportOffline: debounced, no-clear query of PoiIndex.inViewport publishing GooglePoiPins (type -> category label -> pin colour) to the proven GeoJSON pin renderer.
- MapPage: drive onViewportOffline on camera move at zoom>=11; hit-test GOOGLE_POI_LAYER_ID -> toSelectedGooglePoi for the peek pane + Google enrichment; PoiIndex.reload at map-ready so a first-run download isn't left poisoned.
- MyMapLayers: render GooglePoiLayer(googlePins), drop the failing MaPoisLayer(ma_pois) call.
- MapsSearchViewModel: wrap the offline search attempt in runCatching so any failure falls through to Google.

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 c607bc7e3c..c7d9e6131c 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt@@ -76,6 +76,7 @@ import com.vayunmathur.maps.util.SavedPlacesViewModel import com.vayunmathur.maps.util.GooglePoiMapViewModel import com.vayunmathur.maps.util.MapSettingsViewModel import com.vayunmathur.maps.util.MapsSearchViewModel+import com.vayunmathur.maps.util.PoiIndex import com.vayunmathur.maps.util.SelectedFeatureViewModel import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay@@ -146,9 +147,10 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: SelectedFeatureViewModel,     val selectedTransitStop by transitViewModel.selected.collectAsState()     val departuresState by transitViewModel.departures.collectAsState() -    // Retired ambient Google POI pins (P27): the overlay no longer renders these-    // (MyMapLayers draws the baked `ma_pois` layer instead) and no viewport is-    // fed, so this stays empty. Kept for the positional MyMapLayers call.+    // Ambient POI pins (P29): now sourced from the OFFLINE POI index+    // (poi_index.bin) via GooglePoiMapViewModel.onViewportOffline as the camera+    // moves, and rendered by GooglePoiLayer (the proven GeoJSON pin renderer).+    // This replaces the P27 baked `ma_pois` PMTiles layer, which failed to render.     val googlePins by poiViewModel.pins.collectAsState()      // Search-result pins (from the Google search page) drawn on the map.@@ -167,6 +169,9 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: SelectedFeatureViewModel,             val projection = camera.projection             if (projection != null) {                 val bbox = projection.queryVisibleBoundingBox()+                // Ambient POI pins (P29): query the OFFLINE index for the visible+                // box and publish pins for GooglePoiLayer. Only at zoom >= 11.+                poiViewModel.onViewportOffline(bbox.north, bbox.east, bbox.south, bbox.west)                 // Refresh nearby transit stops (P10) only while the layer is on                 // (VM debounces + caches; wide views clear the overlay).                 if (transitEnabled) {@@ -176,11 +181,16 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: SelectedFeatureViewModel,         }     } -    // P27: ambient POIs now come from OUR baked `ma_pois` layer (the v5 PMTiles-    // source-layer), so the Google viewport scrape that used to discover pins as-    // the camera moved is RETIRED — Google is hit only on tap for rich details.-    // The GooglePoiMapViewModel is left wired but never fed a viewport, so no-    // ambient scrape happens.+    // P29: ambient POIs now come from the OFFLINE POI index (poi_index.bin),+    // queried per-viewport and rendered via the proven GooglePoiLayer, replacing+    // the P27 baked `ma_pois` PMTiles layer (which failed to parse/render).+    // Re-map the side files once the map is ready so a first-run download that+    // landed AFTER PoiIndex.initialize first ran (and no-op'd because the files+    // were absent) is picked up instead of staying poisoned. reload() is a cheap+    // idempotent re-mmap. Google is still hit only on tap for rich details.+    LaunchedEffect(Unit) {+        withContext(Dispatchers.IO) { PoiIndex.reload(context) }+    }      // Online-tiles-only: the basemap always streams live (offline zone tile     // packs were removed). Offline ROUTING still works via the downloaded graph.@@ -492,7 +502,7 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: SelectedFeatureViewModel,                                 }                                  // Hit-test the search-result pins first, then the-                                // baked ma_pois layer — a POI tap selects the+                                // ambient offline POI pins — a POI tap selects the                                 // place (name + coord) as a GenericPlace so                                 // SelectedFeatureViewModel.currentPoiInfo fetches                                 // the Google rich details and GooglePoiEnrichment@@ -511,8 +521,8 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: SelectedFeatureViewModel,                                     )?.firstNotNullOfOrNull { it.toSelectedFamilyMember() }                                     ?: projection?.queryRenderedFeatures(                                         offset,-                                        setOf(MA_POIS_LAYER_ID)-                                    )?.firstNotNullOfOrNull { it.toSelectedMaPoi() }+                                        setOf(GOOGLE_POI_LAYER_ID)+                                    )?.firstNotNullOfOrNull { it.toSelectedGooglePoi() }                                 if (pinHit != null) {                                     if (selectedFeature is SpecificFeature.Route) viewModel.setInactiveNavigation(                                         selectedFeature as SpecificFeature.Route
maps/src/main/java/com/vayunmathur/maps/ui/MyMapLayers.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/MyMapLayers.kt b/maps/src/main/java/com/vayunmathur/maps/ui/MyMapLayers.ktindex cf011ae638..5a56edee6b 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/MyMapLayers.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/MyMapLayers.kt@@ -145,12 +145,14 @@ fun MyMapLayers(             TransitLinesLayer(adminSource)         } -        // OUR baked OSM POI overlay (P27): placement/name/type come from the-        // `ma_pois` source-layer in the v5 basemap PMTiles, replacing the Google-        // viewport scrape for ambient pins. Google is hit only on tap for rich-        // details (see MapPage.onMapClick -> toSelectedMaPoi). Reuses the shared-        // vector source; harmless no-op until v5 ships the `ma_pois` layer.-        MaPoisLayer(adminSource)+        // Ambient POI overlay (P29): pins now come from the OFFLINE POI index+        // (poi_index.bin, queried per-viewport by GooglePoiMapViewModel) and are+        // drawn through GooglePoiLayer — the proven GeoJSON pin renderer. This+        // replaces the P27 baked `ma_pois` PMTiles SymbolLayer, which failed to+        // render (PMTiles directory parse error on the overlay source). Google is+        // still hit only on tap for rich details (see MapPage.onMapClick ->+        // toSelectedGooglePoi).+        GooglePoiLayer(googlePins)          // Saved-place pins (Home / Work / starred list). Tap re-selects the         // place → PlaceSheet (Vela's SavedPin).
maps/src/main/java/com/vayunmathur/maps/util/GooglePoiMapViewModel.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/util/GooglePoiMapViewModel.kt b/maps/src/main/java/com/vayunmathur/maps/util/GooglePoiMapViewModel.ktindex acf97f7e8b..a8815b83d4 100644--- a/maps/src/main/java/com/vayunmathur/maps/util/GooglePoiMapViewModel.kt+++ b/maps/src/main/java/com/vayunmathur/maps/util/GooglePoiMapViewModel.kt@@ -7,6 +7,7 @@ import com.vayunmathur.maps.data.google.GooglePoiDiscovery import com.vayunmathur.maps.data.google.GooglePoiPin import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi+import kotlinx.coroutines.Job import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow@@ -83,6 +84,36 @@ class GooglePoiMapViewModel(application: Application) : AndroidViewModel(applica         start()     } +    // Offline ambient-pin refresh (P29), separate from the retired Google-scrape+    // pipeline above. Cancelled on each new viewport so a fast pan debounces.+    private var offlineJob: Job? = null++    /**+     * Publish ambient pins from the OFFLINE POI index (P29) for the+     * [north]..[south] × [west]..[east] box, feeding [GooglePoiLayer] (the proven+     * GeoJSON pin renderer). Debounced; views wider than [MAX_LAT_SPAN] and empty+     * results KEEP the previous pins so a fast pan / wide zoom never blinks the+     * overlay to nothing (P23 no-clear). The index query + name decode run on+     * [Dispatchers.IO]; [PoiIndex.initialize] is idempotent so calling it here is+     * cheap after the first map.+     */+    fun onViewportOffline(north: Double, east: Double, south: Double, west: Double) {+        if (abs(north - south) > MAX_LAT_SPAN) return+        val app = getApplication<Application>()+        offlineJob?.cancel()+        offlineJob = viewModelScope.launch {+            delay(DEBOUNCE_MS)+            val pins = withContext(Dispatchers.IO) {+                runCatching {+                    PoiIndex.initialize(app)+                    PoiIndex.inViewport(west, south, east, north, cap = OFFLINE_CAP)+                        .map { it.toPin() }+                }.getOrDefault(emptyList())+            }+            if (pins.isNotEmpty()) _pins.value = pins+        }+    }+     /**      * Feed a viewport (from `queryVisibleBoundingBox()`). Views wider than      * [MAX_LAT_SPAN] suppress fetching but KEEP the last pins (no clear);@@ -149,6 +180,16 @@ class GooglePoiMapViewModel(application: Application) : AndroidViewModel(applica         }     } +    /** Map an offline index record to the pin model [GooglePoiLayer] renders; the+     *  category label drives its data-driven pin colour (P29). */+    private fun PoiIndex.PoiRecord.toPin(): GooglePoiPin = GooglePoiPin(+        id = "poi:$latE7:$lonE7",+        name = name,+        lat = lat,+        lng = lon,+        category = PoiCategories.label(type),+    )+     /** Rough planar metres between two lat/lon points — enough for a move      *  threshold; no need for full haversine at these small spans. */     private fun metersBetween(aLat: Double, aLon: Double, bLat: Double, bLon: Double): Double {@@ -195,6 +236,10 @@ class GooglePoiMapViewModel(application: Application) : AndroidViewModel(applica         // ~16 km of latitude — roughly city-zoom; wider views skip the scrape.         const val MAX_LAT_SPAN = 0.15 +        // Cap on ambient offline pins per viewport (P29) so a dense city box+        // stays bounded; matches GooglePoiLayer's close-zoom feel.+        const val OFFLINE_CAP = 300+         // Degrees of latitude → metres, for turning the viewport's lat span into         // the ground span the discovery fetch tightens its `!1d` window to.         const val METERS_PER_DEG_LAT = 111_320.0
maps/src/main/java/com/vayunmathur/maps/util/MapsSearchViewModel.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/util/MapsSearchViewModel.kt b/maps/src/main/java/com/vayunmathur/maps/util/MapsSearchViewModel.ktindex 76cf146600..594a1bf7a6 100644--- a/maps/src/main/java/com/vayunmathur/maps/util/MapsSearchViewModel.kt+++ b/maps/src/main/java/com/vayunmathur/maps/util/MapsSearchViewModel.kt@@ -96,11 +96,15 @@ class MapsSearchViewModel(application: Application) : AndroidViewModel(applicati             delay(250)             // Try the offline OSM POI index first (P27): resolving a POI name             // locally avoids a Google call. Google stays the fallback (and-            // handles addresses, which the POI index doesn't carry).+            // handles addresses, which the POI index doesn't carry). Any offline+            // failure (unmapped/poisoned index, decode error) is swallowed so the+            // query still falls through to Google.             val app = getApplication<Application>()             val offline = withContext(Dispatchers.IO) {-                PoiIndex.initialize(app)-                PoiIndex.searchByName(query, nearLat, nearLon, limit = 20)+                runCatching {+                    PoiIndex.initialize(app)+                    PoiIndex.searchByName(query, nearLat, nearLon, limit = 20)+                }.getOrDefault(emptyList())             }             if (offline.isNotEmpty()) {                 _results.value = offline.map { it.toSearchResult() }
maps/src/main/java/com/vayunmathur/maps/util/PoiIndex.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/util/PoiIndex.kt b/maps/src/main/java/com/vayunmathur/maps/util/PoiIndex.ktindex 05021bcfed..7d0d5ae9ea 100644--- a/maps/src/main/java/com/vayunmathur/maps/util/PoiIndex.kt+++ b/maps/src/main/java/com/vayunmathur/maps/util/PoiIndex.kt@@ -240,6 +240,39 @@ object PoiIndex {         }     } +    /**+     * All POIs whose coordinate falls inside the [west]..[east] × [south]..[north]+     * bounding box, capped at [cap] (a linear scan over the flat record array —+     * cheap even at ~283k records for a per-idle viewport refresh). Names are+     * decoded for each returned record. Empty when the index isn't loaded; used+     * to drive the ambient offline pin overlay (P29).+     */+    @Synchronized+    fun inViewport(+        west: Double,+        south: Double,+        east: Double,+        north: Double,+        cap: Int = 300,+    ): List<PoiRecord> {+        if (!loaded || cap <= 0) return emptyList()+        val minLatE7 = (south * 1e7).toInt()+        val maxLatE7 = (north * 1e7).toInt()+        val minLonE7 = (west * 1e7).toInt()+        val maxLonE7 = (east * 1e7).toInt()+        val out = ArrayList<PoiRecord>(minOf(cap, count))+        var i = 0+        while (i < count && out.size < cap) {+            val latE7 = recLatE7(i)+            val lonE7 = recLonE7(i)+            if (latE7 in minLatE7..maxLatE7 && lonE7 in minLonE7..maxLonE7) {+                out.add(PoiRecord(latE7, lonE7, recType(i), nameAt(recNameOff(i)) ?: ""))+            }+            i+++        }+        return out+    }+     private class Ranked(val record: PoiRecord, val rank: Int, val distSq: Double)     private class Hit(val idx: Int, val distSq: Double)