From 8e547570e1ae89fc3387584000b0659743b99099 Mon Sep 17 00:00:00 2001 From: androidlover5842 Date: Sat, 31 Jan 2026 11:00:30 +0530 Subject: [PATCH] Improve pin geocoding for city/state --- .../component/GoogleGeocodingClient.kt | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/android/trisolarisserver/component/GoogleGeocodingClient.kt b/src/main/kotlin/com/android/trisolarisserver/component/GoogleGeocodingClient.kt index 278dcac..f983649 100644 --- a/src/main/kotlin/com/android/trisolarisserver/component/GoogleGeocodingClient.kt +++ b/src/main/kotlin/com/android/trisolarisserver/component/GoogleGeocodingClient.kt @@ -22,7 +22,8 @@ class GoogleGeocodingClient( if (apiKey.isBlank()) return null return try { val url = UriComponentsBuilder.fromUriString(baseUrl) - .queryParam("address", "${pinCode} India") + .queryParam("components", "postal_code:$pinCode|country:IN") + .queryParam("region", "IN") .queryParam("key", apiKey) .toUriString() val response = restTemplate.getForEntity(url, String::class.java) @@ -38,7 +39,10 @@ class GoogleGeocodingClient( val root = objectMapper.readTree(body) val results = root.path("results") 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 var city: String? = null @@ -47,6 +51,12 @@ class GoogleGeocodingClient( val types = comp.path("types").mapNotNull { it.asText(null) }.toSet() when { "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) { city = comp.path("long_name").asText(null) }