Prefer main city from postcode localities
All checks were successful
build-and-deploy / build-deploy (push) Successful in 32s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 32s
This commit is contained in:
@@ -68,6 +68,15 @@ class GoogleGeocodingClient(
|
|||||||
val resultNode = results.firstOrNull { node ->
|
val resultNode = results.firstOrNull { node ->
|
||||||
node.path("types").any { it.asText(null) == "postal_code" }
|
node.path("types").any { it.asText(null) == "postal_code" }
|
||||||
} ?: results.first()
|
} ?: 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")
|
val components = resultNode.path("address_components")
|
||||||
if (!components.isArray) return null
|
if (!components.isArray) return null
|
||||||
|
|
||||||
@@ -92,6 +101,18 @@ class GoogleGeocodingClient(
|
|||||||
if (preferredCity == null && state.isNullOrBlank()) return null
|
if (preferredCity == null && state.isNullOrBlank()) return null
|
||||||
return listOfNotNull(preferredCity, state?.trim()?.ifBlank { null }).joinToString(", ")
|
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(
|
data class GeocodeResult(
|
||||||
|
|||||||
Reference in New Issue
Block a user