Last active
January 8, 2021 05:19
-
-
Save rommansabbir/519ad699e00642adcb4684f342d9f1de to your computer and use it in GitHub Desktop.
Transform Place API to Address API - Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful extension!