Modern Apps git

← Modern-Apps.git

networklocation: match structured geocode queries with house numbers

Commit
83629a94810fef2ca351714f2bba9413adf99edd
Author
Vayun Mathur <[email protected]>
When
2026-11-07 20:10 UTC (28 days ago)
Parents
f5b72a0
Symptom: a forward-geocode query written the way callers normally
write an address - '124 Main St, Springfield, CA, US' - returned no
results, while the same query without the number worked.

Root cause: onGetFromLocationName passed the whole street part,
house number included, to the native forward index. That index keys
streets by name alone; the house number is a separate column (the
renderer rebuilds '124 Main St' from the two), so the exact
dictionary lookup for '124 Main St' always missed and the query
silently came back empty.

Fix: try the street verbatim first, and on a miss retry once with a
leading house-number token (digits, optionally one letter suffix)
stripped. Numbered street names such as '5th Avenue' are untouched.

Verification: new regression test 'structured name query with a
house number still matches the street' requires both fixture
addresses back, the second rendered as '124 Main St'; module unit
tests and compile run after this change.

Diff

networklocation/src/main/java/com/vayunmathur/networklocation/GeocodeService.kt
diff --git a/networklocation/src/main/java/com/vayunmathur/networklocation/GeocodeService.kt b/networklocation/src/main/java/com/vayunmathur/networklocation/GeocodeService.ktindex 4eef03a845..0bb2deb4bf 100644--- a/networklocation/src/main/java/com/vayunmathur/networklocation/GeocodeService.kt+++ b/networklocation/src/main/java/com/vayunmathur/networklocation/GeocodeService.kt@@ -55,15 +55,19 @@ class GeocodeService : Service() {                 val parts = locationName.split(",").map { it.trim() }.filter { it.isNotEmpty() }                  // A fully structured query is answered from the address index, which is exact.+                // Callers write the street part as "<number> <street>", but the index keys+                // streets by name alone (the house number is a separate column), so retry+                // without a leading house number when the verbatim street misses.                 val flat = if (parts.size >= 4) {-                    GeocoderNative.forward(-                        handle,-                        country = parts[parts.size - 1],-                        state = parts[parts.size - 2],-                        city = parts[parts.size - 3],-                        street = parts[parts.size - 4],-                        limit = limit,-                    )+                    val country = parts[parts.size - 1]+                    val state = parts[parts.size - 2]+                    val city = parts[parts.size - 3]+                    val street = parts[parts.size - 4]+                    GeocoderNative.forward(handle, country, state, city, street, limit)+                        ?.takeIf { it.isNotEmpty() }+                        ?: streetWithoutHouseNumber(street)?.let {+                            GeocoderNative.forward(handle, country, state, city, it, limit)+                        }                 } else {                     // Anything else is treated as a feature name. v2 had no name column, so a                     // query like "Golden Gate Park" could not be answered at all and this@@ -82,6 +86,17 @@ class GeocodeService : Service() {         }     } +    /** "124 Main St" -> "Main St"; null when there is no leading house-number token. */+    private fun streetWithoutHouseNumber(street: String): String? {+        val firstSpace = street.indexOf(' ')+        if (firstSpace <= 0) return null+        val head = street.substring(0, firstSpace)+        if (!head.all { it.isDigit() } && !(head.length > 1 && head.dropLast(1).all { it.isDigit() })) {+            return null+        }+        return street.substring(firstSpace + 1).trim().takeIf { it.isNotEmpty() }+    }+     override fun onCreate() {         super.onCreate()         openDatabase()
networklocation/src/test/java/com/vayunmathur/networklocation/GeocodeServiceTest.kt
diff --git a/networklocation/src/test/java/com/vayunmathur/networklocation/GeocodeServiceTest.kt b/networklocation/src/test/java/com/vayunmathur/networklocation/GeocodeServiceTest.ktindex 59f72f1d9c..a4c4abce4a 100644--- a/networklocation/src/test/java/com/vayunmathur/networklocation/GeocodeServiceTest.kt+++ b/networklocation/src/test/java/com/vayunmathur/networklocation/GeocodeServiceTest.kt@@ -109,6 +109,20 @@ class GeocodeServiceTest {         controller.destroy()     } +    @Test+    fun `structured name query with a house number still matches the street`() {+        installDb()+        val controller = Robolectric.buildService(GeocodeService::class.java).create()+        val addrs = mutableListOf<Address>()+        val err = controller.get().provider.onGetFromLocationName(+            "124 Main St, Springfield, CA, US", 0.0, 0.0, 0.0, 0.0, 10, params(), addrs,+        )+        assertNull(err)+        assertEquals(2, addrs.size, "house number must not defeat the street lookup")+        assertEquals("124 Main St", addrs[1].getAddressLine(0))+        controller.destroy()+    }+     @Test     fun `unstructured name query uses the name index`() {         installDb()