How can i find City name from Longitude and Latitude Kotlin

Issue

In kotlin how do I get the city name of the current location from which I get the latitude and longitude values?

Solution

First I tried to get city name with Adress[0].locality but returned null. Adress[0].adminArea gave the name of the city whose latitude and longitude I have.

private fun getCityName(lat: Double,long: Double):String{
        val cityName: String?
        val geoCoder = Geocoder(requireContext(), Locale.getDefault())
        val Adress = geoCoder.getFromLocation(lat,long,3)

        cityName = Adress[0].adminArea
       binding.txtCity.text = cityName
        return cityName
    }

Answered By – Hakan Ulaş

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *