TheJadav

Android – Get address using lattitude and longitude (Lat, Lng) using Geocoder

Geocoder class can be helper class which is useful for geocoding or reverse geocoding, that means we get lattitude and longitude from address or vica versa. We need one location to get addess. If you don't know how to get current location

Geocoder

Geocoder class can be helper class which is useful for geocoding or reverse geocoding, that means we get lattitude and longitude from address or vica versa.
We need one location to get addess. If you don’t know how to get current location VISIT HERE.
Once we get location, it is easy to get location from Geocoder. First of all initialize Geocoder. I have created one variable.

val geoCoder = Geocoder(context, Locale.getDefault())

Retrive address from geocoder

        var addresses: List<Address> = emptyList()
        try {
            addresses = geoCoder.getFromLocation(position.latitude, position.longitude, 1)
        } catch (e: IOException) {
            e.printStackTrace()
        } catch (e: IllegalArgumentException){
            e.printStackTrace()
        }

This will return list of Address. ‘Address’ is class from ‘android.location’ package so we don’t need to create it. Now, to display address, we need to get information from addresses

        if(addresses.isEmpty()){

        } else {
            val address = addresses[0]
            val addressFragment = with(address){
                (0..maxAddressLineIndex).map { getAddressLine(it) }
            }
            //this displays first address, but you can loop it and display all addresses.

            tvAddress.text = addressFragment.joinToString("\n")
        }

Here is one image that shows the address return from one location:

Android – Get address using lattitude and longitude (Lat, Lng) using Geocoder

Share this content:

Share:

More Posts

Introduction to Kotlin: A Versatile and Modern Programming Language

Kotlin, a versatile and modern programming language, offers developers a concise, safe, and interoperable coding experience. With features like null safety, extension functions, and coroutines, Kotlin enhances productivity and readability, making it an attractive choice for Android development and beyond. Whether you’re new to programming or an experienced developer, exploring Kotlin opens up a world of possibilities for building robust and expressive applications.

Mastering the Android Activity Lifecycle: A Comprehensive Guide

The Android Activity Lifecycle is a fundamental concept for Android developers. It defines how an Activity behaves during its lifecycle, from creation to destruction. Understanding the Activity Lifecycle is crucial for managing resources efficiently and delivering a smooth user experience. In this blog post, we’ll briefly introduce the different states an Activity can be in and the main callback methods associated with each state. Let’s dive in and explore this important aspect of Android app development!

Table of Contents

Send Us A Message