I have done as below in Kotlin to get the Address from LatLng using GeoCoder in Android :
private fun getAddress(latLng: LatLng): String {
// 1
val geocoder = Geocoder(this)
val addresses: List<Address>?
val address: Address?
var addressText = ""
try {
// 2
addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1)
// 3
if (null != addresses && !addresses.isEmpty()) {
address = addresses[0]
for (i in 0 until address.maxAddressLineIndex) {
addressText += if (i == 0) address.getAddressLine(i) else "\n" + address.getAddressLine(i)
}
}
} catch (e: IOException) {
Log.e("MapsActivity", e.localizedMessage)
}
Log.e("ADDRESS TEXT >>",""+addressText)
return addressText
}
But, Am getting 0 for address.maxAddressLineIndex.
address and addresses[0] has the values. I checked by debugging it.
And that's why my for loop is not executing inside.
What might be the issue? Any Solution please.