-3

this is my fuction

private fun getAddress(latLng: LatLng): String {
        val geocoder = Geocoder(this)
        val addresses: List<Address>?
        val address: Address?
        var addressText = ""

        try {
            addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1)
            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)
        }

        return addressText
}

i call fuction like this

val lat  = dataVar.get(0).lat!!.toDouble()
val lang = dataVar.get(0).lng!!.toDouble()

val latLang = LatLng(lat, lang)

address.text = getAddress(latLang)

output not showing anything. am using API for free, or need active billing to use it??

2019-11-30 09:23:28.224 7918-7918/ E/SchedPolicy: set_timerslack_ns write failed: Operation not permitted

sulistyo adi
  • 1
  • 1
  • 3
  • 2
    Please post your code inline using the editor, clicking through to multiple images makes it very difficult for others to fully analyse your code. Additionally what do the logs output? I'd also like to know whether the "it" value you are passing to the function is valid, have you an example of that data (perhaps also log that and let us know the output). – Scott Evans Nov 29 '19 at 01:37

1 Answers1

2

You can change your getAddress function to be

private fun getAddress(lat: Double, lng: Double): String {
    val geocoder = Geocoder(this)
    val list = geocoder.getFromLocation(lat, lng, 1)
    return list[0].getAddressLine(0)
}

then the example of calling the method is like this

val lat = -7.316463
val lng = 112.748348

val address = getAddress(lat, lng)
// use address variable for your purpose

Asnwering the question of google services. If you only using map, its free for now. If you are using places API, it needs to enable account billing. This link may help to start enable your billing

Erwin Kurniawan A
  • 958
  • 2
  • 9
  • 17