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?