Improve pin geocoding for city/state
All checks were successful
build-and-deploy / build-deploy (push) Successful in 37s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 37s
This commit is contained in:
@@ -22,7 +22,8 @@ class GoogleGeocodingClient(
|
|||||||
if (apiKey.isBlank()) return null
|
if (apiKey.isBlank()) return null
|
||||||
return try {
|
return try {
|
||||||
val url = UriComponentsBuilder.fromUriString(baseUrl)
|
val url = UriComponentsBuilder.fromUriString(baseUrl)
|
||||||
.queryParam("address", "${pinCode} India")
|
.queryParam("components", "postal_code:$pinCode|country:IN")
|
||||||
|
.queryParam("region", "IN")
|
||||||
.queryParam("key", apiKey)
|
.queryParam("key", apiKey)
|
||||||
.toUriString()
|
.toUriString()
|
||||||
val response = restTemplate.getForEntity(url, String::class.java)
|
val response = restTemplate.getForEntity(url, String::class.java)
|
||||||
@@ -38,7 +39,10 @@ class GoogleGeocodingClient(
|
|||||||
val root = objectMapper.readTree(body)
|
val root = objectMapper.readTree(body)
|
||||||
val results = root.path("results")
|
val results = root.path("results")
|
||||||
if (!results.isArray || results.isEmpty) return null
|
if (!results.isArray || results.isEmpty) return null
|
||||||
val components = results.first().path("address_components")
|
val resultNode = results.firstOrNull { node ->
|
||||||
|
node.path("types").any { it.asText(null) == "postal_code" }
|
||||||
|
} ?: results.first()
|
||||||
|
val components = resultNode.path("address_components")
|
||||||
if (!components.isArray) return null
|
if (!components.isArray) return null
|
||||||
|
|
||||||
var city: String? = null
|
var city: String? = null
|
||||||
@@ -47,6 +51,12 @@ class GoogleGeocodingClient(
|
|||||||
val types = comp.path("types").mapNotNull { it.asText(null) }.toSet()
|
val types = comp.path("types").mapNotNull { it.asText(null) }.toSet()
|
||||||
when {
|
when {
|
||||||
"locality" in types -> city = comp.path("long_name").asText(null) ?: city
|
"locality" in types -> city = comp.path("long_name").asText(null) ?: city
|
||||||
|
"postal_town" in types -> if (city == null) {
|
||||||
|
city = comp.path("long_name").asText(null)
|
||||||
|
}
|
||||||
|
"sublocality" in types -> if (city == null) {
|
||||||
|
city = comp.path("long_name").asText(null)
|
||||||
|
}
|
||||||
"administrative_area_level_2" in types -> if (city == null) {
|
"administrative_area_level_2" in types -> if (city == null) {
|
||||||
city = comp.path("long_name").asText(null)
|
city = comp.path("long_name").asText(null)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user