简体   繁体   English

我如何从经度和纬度 Kotlin 中找到城市名称

[英]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?在 kotlin 如何获取当前位置的城市名称,从中获取纬度和经度值?

First I tried to get city name with Adress[0].locality but returned null.首先,我尝试使用 Adress[0].locality 获取城市名称,但返回 null。 Adress[0].adminArea gave the name of the city whose latitude and longitude I have. Adress[0].adminArea 给出了我所拥有的经纬度城市的名称。

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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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