简体   繁体   中英

How can i find City name from Longitude and Latitude Kotlin

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

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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM