Skip to content

Commit 379f25d

Browse files
authored
fix: Improved place parser (#778)
* fix: Improved place parser
1 parent c093ea1 commit 379f25d

File tree

1 file changed

+58
-26
lines changed

1 file changed

+58
-26
lines changed

demo-kotlin/app/src/main/java/com/example/placesdemo/AutocompleteAddressActivity.kt

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ import com.google.android.gms.maps.model.LatLng
4040
import com.google.android.gms.maps.model.MapStyleOptions
4141
import com.google.android.gms.maps.model.Marker
4242
import com.google.android.gms.maps.model.MarkerOptions
43+
import com.google.android.libraries.places.api.model.AddressComponents
4344
import com.google.android.libraries.places.api.model.Place
4445
import com.google.android.libraries.places.api.model.PlaceTypes
4546
import com.google.android.libraries.places.widget.Autocomplete
4647
import com.google.android.libraries.places.widget.model.AutocompleteActivityMode
4748
import com.google.maps.android.SphericalUtil.computeDistanceBetween
48-
import java.util.*
4949

5050
/**
5151
* Activity for using Place Autocomplete to assist filling out an address form.
@@ -197,38 +197,19 @@ class AutocompleteAddressActivity : AppCompatActivity(R.layout.autocomplete_addr
197197

198198
private fun fillInAddress(place: Place) {
199199
val components = place.addressComponents
200-
val address1 = StringBuilder()
201-
val postcode = StringBuilder()
202200

203201
// Get each component of the address from the place details,
204202
// and then fill-in the corresponding field on the form.
205203
// Possible AddressComponent types are documented at https://goo.gle/32SJPM1
206204
if (components != null) {
207-
for (component in components.asList()) {
208-
when (component.types[0]) {
209-
"street_number" -> {
210-
address1.insert(0, component.name)
211-
}
212-
"route" -> {
213-
address1.append(" ")
214-
address1.append(component.shortName)
215-
}
216-
"postal_code" -> {
217-
postcode.insert(0, component.name)
218-
}
219-
"postal_code_suffix" -> {
220-
postcode.append("-").append(component.name)
221-
}
222-
"locality" -> binding.autocompleteCity.setText(component.name)
223-
"administrative_area_level_1" -> {
224-
binding.autocompleteState.setText(component.shortName)
225-
}
226-
"country" -> binding.autocompleteCountry.setText(component.name)
227-
}
205+
with (components.toAddress()) {
206+
binding.autocompleteAddress1.setText(streetAddress)
207+
binding.autocompletePostal.setText(fullPostalCode)
208+
binding.autocompleteCity.setText(locality)
209+
binding.autocompleteState.setText(adminArea)
210+
binding.autocompleteCountry.setText(country)
228211
}
229212
}
230-
binding.autocompleteAddress1.setText(address1.toString())
231-
binding.autocompletePostal.setText(postcode.toString())
232213

233214
// After filling the form with address components from the Autocomplete
234215
// prediction, set cursor focus on the second address line to encourage
@@ -335,4 +316,55 @@ class AutocompleteAddressActivity : AppCompatActivity(R.layout.autocomplete_addr
335316
private val TAG = AutocompleteAddressActivity::class.java.simpleName
336317
private const val MAP_FRAGMENT_TAG = "MAP"
337318
}
319+
}
320+
321+
/**
322+
* Data class representing a postal address.
323+
*
324+
* @property streetNumber The street number of the address.
325+
* @property locality The locality or neighborhood of the address.
326+
* @property route The street name or route of the address.
327+
* @property postCode The primary part of the postal code.
328+
* @property postCodeSuffix The optional suffix or extension of the postal code.
329+
* @property adminArea The administrative area, such as state, province, or region.
330+
* @property country The country of the address.
331+
*/
332+
private data class Address(
333+
val streetNumber: String,
334+
val locality: String,
335+
val route: String,
336+
val postCode: String,
337+
val postCodeSuffix: String,
338+
val adminArea: String,
339+
val country: String
340+
) {
341+
val fullPostalCode: String
342+
get() = listOf(postCode, postCodeSuffix).filter { it.isNotBlank() }.joinToString("-")
343+
val streetAddress: String
344+
get() = listOf(streetNumber, route).filter { it.isNotBlank() }.joinToString(" ")
345+
}
346+
347+
/**
348+
* Converts an [AddressComponents] object to an [Address] object.
349+
*
350+
* This function iterates through the address components, creating a map where the key is the component type
351+
* (e.g., "street_number", "route") and the value is the component name. It then uses this map to populate
352+
* the fields of an [Address] object.
353+
*
354+
* If a specific address component type is not found in the [AddressComponents], the corresponding field in
355+
* the [Address] object will be set to an empty string.
356+
*
357+
* @return An [Address] object representing the address information extracted from the [AddressComponents].
358+
*/
359+
private fun AddressComponents.toAddress(): Address {
360+
val addressMap = this.asList().associate { it.types[0] to it.name }
361+
return Address(
362+
streetNumber = addressMap["street_number"] ?: "",
363+
route = addressMap["route"] ?: "",
364+
postCode = addressMap["postal_code"] ?: "",
365+
postCodeSuffix = addressMap["postal_code_suffix"] ?: "",
366+
locality = addressMap["locality"] ?: "",
367+
adminArea = addressMap["administrative_area_level_1"] ?: "",
368+
country = addressMap["country"] ?: ""
369+
)
338370
}

0 commit comments

Comments
 (0)