diff --git a/src/main/kotlin/com/android/trisolarisserver/component/GoogleGeocodingClient.kt b/src/main/kotlin/com/android/trisolarisserver/component/GoogleGeocodingClient.kt index f983649..34805d5 100644 --- a/src/main/kotlin/com/android/trisolarisserver/component/GoogleGeocodingClient.kt +++ b/src/main/kotlin/com/android/trisolarisserver/component/GoogleGeocodingClient.kt @@ -46,6 +46,7 @@ class GoogleGeocodingClient( if (!components.isArray) return null var city: String? = null + var admin2: String? = null var state: String? = null for (comp in components) { val types = comp.path("types").mapNotNull { it.asText(null) }.toSet() @@ -57,13 +58,12 @@ class GoogleGeocodingClient( "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) - } + "administrative_area_level_2" in types -> admin2 = comp.path("long_name").asText(null) ?: admin2 "administrative_area_level_1" in types -> state = comp.path("long_name").asText(null) ?: state } } - if (city.isNullOrBlank() && state.isNullOrBlank()) return null - return listOfNotNull(city?.trim()?.ifBlank { null }, state?.trim()?.ifBlank { null }).joinToString(", ") + val preferredCity = admin2?.trim()?.ifBlank { null } ?: city?.trim()?.ifBlank { null } + if (preferredCity == null && state.isNullOrBlank()) return null + return listOfNotNull(preferredCity, state?.trim()?.ifBlank { null }).joinToString(", ") } }