Skip to content

Instantly share code, notes, and snippets.

@rommansabbir
Last active January 8, 2021 05:19
Show Gist options
  • Save rommansabbir/519ad699e00642adcb4684f342d9f1de to your computer and use it in GitHub Desktop.
Save rommansabbir/519ad699e00642adcb4684f342d9f1de to your computer and use it in GitHub Desktop.
Transform Place API to Address API - Android
/**
* This function transform [Place] into [Address]
*/
fun Place.toAddress(): Address {
val address = Address(Locale.ENGLISH)
this.latLng?.let {
address.latitude = it.latitude
address.longitude = it.longitude
}
when (addressComponents) {
null -> {
throw Exception("Address component is null")
}
else -> {
addressComponents?.asList()?.forEach { component ->
component.types.forEach {
when (it) {
"street_number" -> {
address.featureName = component.name
}
"route" -> {
val tempFeatureName = address.featureName
address.featureName = "${tempFeatureName}, ${component.name}"
}
"locality" -> {
address.locality = component.name
}
"administrative_area_level_1" -> {
address.adminArea = component.shortName
}
"postal_code" -> {
address.postalCode = component.name
}
"country" -> {
address.countryName = component.name
address.countryCode = component.shortName
}
}
}
}
}
}
return address
}
@goforbg
Copy link

goforbg commented Jan 8, 2021

Useful extension!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment