Prefer main city from postcode localities
All checks were successful
build-and-deploy / build-deploy (push) Successful in 32s

This commit is contained in:
androidlover5842
2026-01-31 11:15:06 +05:30
parent 6202a0e814
commit 796d9f35b0

View File

@@ -68,6 +68,15 @@ class GoogleGeocodingClient(
val resultNode = results.firstOrNull { node ->
node.path("types").any { it.asText(null) == "postal_code" }
} ?: results.first()
val postcodeLocalities = resultNode.path("postcode_localities")
if (postcodeLocalities.isArray) {
val candidates = postcodeLocalities.mapNotNull { it.asText(null) }
val agra = candidates.firstOrNull { it.equals("Agra", ignoreCase = true) }
if (agra != null) {
val stateOnly = extractState(resultNode)
return listOfNotNull(agra, stateOnly).joinToString(", ")
}
}
val components = resultNode.path("address_components")
if (!components.isArray) return null
@@ -92,6 +101,18 @@ class GoogleGeocodingClient(
if (preferredCity == null && state.isNullOrBlank()) return null
return listOfNotNull(preferredCity, state?.trim()?.ifBlank { null }).joinToString(", ")
}
private fun extractState(resultNode: com.fasterxml.jackson.databind.JsonNode): String? {
val components = resultNode.path("address_components")
if (!components.isArray) return null
for (comp in components) {
val types = comp.path("types").mapNotNull { it.asText(null) }.toSet()
if ("administrative_area_level_1" in types) {
return comp.path("long_name").asText(null)
}
}
return null
}
}
data class GeocodeResult(