How to Call Geocoder.getFromLocation Correctly

Oct 5, 2018
Convert GPS latitude and longitude to address

There are 2 things to be aware of:

  • The returned values may be obtained by means of a network lookup. It may be useful to call this method from a thread separate from your primary UI thread.
  • Possible IOException if the network is unavailable or any other I/O problem occurs
  • The returned addresses will be localized for the locale provided to this class's constructor.
launch {    val geocoder = Geocoder(context, Locale.US)    try {        val results = geocoder.getFromLocation(lat, lng, 1)        if (results.size > 0) {            val address = results[0]            // so something        }    }    catch (e: IOException) {        e.printStackTrace()    }}

NOTE: I use android kotlin coroutine to launch the code in background coroutine.

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.