Show physical address in Google Maps API

I’m developing a Kotlin application using Google Maps API. I would like to show into snippet or title the physical address when is touching the Marker.

The application allows create markers when is long pressed (Using setOnMapClickListener). After this you can see the marker latitude and longitude when is clicked like the next picture:

My mainActivity.kt where is creating the markers is:

override fun onMapReady(googleMap: GoogleMap) {
        mMap = googleMap //Inicializa el mapa
        mMap.uiSettings.isZoomControlsEnabled = true //Zoom en el mapa
        mMap.moveCamera(CameraUpdateFactory.zoomBy(17f)) //Distancia de acercamiento

     //Activity below is used to create the markers when is long pressed
        mMap.setOnMapLongClickListener { latlng ->
            mMap.animateCamera(CameraUpdateFactory.newLatLng(latlng))
            getcoordinates = LatLng(latlng.latitude, latlng.longitude) //catch coordinates from 
maps API

        val markerOptions = MarkerOptions().position(getcoordinates)
        val titlesr = getAddress(getcoordinates)
        markerOptions.title(titlesr)

        mMap.addMarker(MarkerOptions() //Aquí crea los marcadores
            .position(getcoordinates)
            .title(getcoordinates.toString())
            .snippet("I want to show my Physical address here"))
    }
    setUpMap()
}

Aditionally, I followed the steps of these threads: Thread 1 Thread 2 Thread 3

I had issues to followed the first both threads steps, because they’re in Java language, even I translated from Java to Kotlin on Android Studio function, and using Kotlin translator but some functions are deprecated or is so hard adapt them to my code. The third one, I was able to create a function to getAddress but is not clear the unique answer. So I don’t know how to implement to my onMapReady markers to show the physical location, I tried to create the next function:

private fun getAddress(lat: Double, lng: Double): String? {
    val geocoder = Geocoder(this)
    val list = geocoder.getFromLocation(lat,lng,1)
    return list[0].getAddressLine(0)
}

How to achieve this?

Using the same function:

private fun getAddress(lat: LatLng): String? {
        val geocoder = Geocoder(this)
        val list = geocoder.getFromLocation(lat.latitude, lat.longitude,1)
        return list[0].getAddressLine(0)
    }

I’ve solved. Only I converted the objetcs to LatLng data. and I encapsulated to my mapOnReady

If anyone has the same issue this is my mapOnReady code:

mMap.setOnMapLongClickListener { latlng ->
//mMap.clear(); -Let only one marker
mMap.animateCamera(CameraUpdateFactory.newLatLng(latlng))
//mMap.setOnInfoWindowClickListener()
getcoordinates = LatLng(latlng.latitude, latlng.longitude) //catch coordinates from maps API

        val markerOptions = MarkerOptions().position(getcoordinates)
        /*val titlesr = getAddress(getcoordinates)
        markerOptions.title(titlesr)*/
        val titlesr = getAddress(getcoordinates)
        markerOptions.title(titlesr)

        //.title(mMap.addMarker(markerOptions).toString()))
        mMap.addMarker(MarkerOptions()
            .position(getcoordinates)
            //.title(getcoordinates.toString()) -->ONLY SHOW THE LATITUDE AND LONGITUDE
            .title(getAddress(getcoordinates))
            .snippet("La ubicación puede ser precisa más no exacta."))
        //.title("Hola") --Basic marker interaction with user
    }

And put correctly the objects with correct encapsulation, and declaration on each other.

Thank you.