0

In my program, I want to get a string with the name of the city that corresponds to the location on the phone. I have the geocoder code:

class LocationData : LocationDataInterface{
    var result: String = ""
     override fun getAdreesTown(location: Location, context: Context): String {
         val thread = Thread(
             Runnable {
                 val geocoder = Geocoder(context, Locale.getDefault())
                 try{
                     var list: List<Address> = geocoder. getFromLocation(location.getLatitude(), location.getLongitude(), 1)
                     if(list != null && list.size > 0){
                         var address : Address = list.get(0) as Address
                         result = "${address. getAddressLine(0)}  , ${address.getLocality()}"
                     }
                 }catch(e: IOException){
                     Log.d("Egor", "Подключение к геокодеру не установлено: $e.message")
             }
             }).run()
         thread.start()
         return result
         }
    }

Permission to request location is specified in the Manifest:

<uses-permission android:name = "android.permission.ACCESS_COARSE_LOCATION"/>

My problem is that I do not know how to get the Location object with the current location to pass to the Geocoder. I tried different options (for example, fusedLocationClient), but nothing worked.

class MainActivity : AppCompatActivity(), DataProcessingCallback {
\\ 
lateinit var locationData: LocationDataInterface
\\
override fun onCreate(savedInstanceState: Bundle?) {
locationData = LocationData()
val e = locationData.getAdreesTown(Location(LocationManager.NETWORK_PROVIDER), this)
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
        fusedLocationClient.lastLocation.addOnSuccessListener { location : Location? ->
            locationNow = location
        }
        val e = locationData.getAdreesTown(location, this)

How do I create a location object with the current location?

Egor0702
  • 15
  • 2
  • Does this answer your question? [What is the simplest and most robust way to get the user's current location on Android?](https://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-on-a) – Markus Kauppinen Jun 09 '21 at 12:50
  • _"My problem is that I do not know how to get the Location object with the current location to pass to the Geocoder."_ The problem with the question is that it doesn't show what you tried so we can't say what went wrong. _"Permission to request location is specified in the Manifest:"_ That's not enough in modern Android versions. We have "runtime permissions" now. _"I tried different options (for example, fusedLocationClient), but nothing worked. "_ And without more information we can't tell why they didn't work. – Markus Kauppinen Jun 09 '21 at 12:52
  • There are many related answers on Stack Overflow. All are not very good or really up-to-date. Some can be confusing too. And the one I linked isn't necessarily the best one, but I found it easily. It's better to look at the actual Android documentation to get a proper understanding on the topic: [https://developer.android.com/training/location](https://developer.android.com/training/location) – Markus Kauppinen Jun 09 '21 at 12:55

0 Answers0