I will provide a code that returns the address and you can edit to get the information you want, but it is plugAndPlay. Come on.
Declare these variables before your onCreate.
private lateinit var fusedLocationClient: FusedLocationProviderClient
private lateinit var lastLocation: Location
On onCreate initialize this variable:
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
In the override fun method onMapReady(Googlemap: Googlemap) { what android Studio creates for you, call the following method:
getUserLocation()
Now let’s create the getUserLocation method:
private fun getUserLocation (){
// 1
mMap.isMyLocationEnabled = true //libera buscar a localização do usuário
// 2
fusedLocationClient.lastLocation.addOnSuccessListener(this) { location -> //pega a localização
// Got last known location. In some rare situations this can be null.
// 3
if (location != null) { //se encontrou algo, vai marcar a posição no mapa.
lastLocation = location
val currentLatLng = LatLng(location.latitude, location.longitude)
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 17f))
val mTxt = findViewById<TextView>(R.id.mTxtMsg) //objeto que vai receber o endereço
mTxt.setText(getAddress(currentLatLng, location.latitude, location.longitude)) //coloca o endereço dentro do objeto através deste método.
}
}
}
Finally we will create the method that actually fetches the user address data.
private fun getAddress(latLng: Latlng, lat: Double, long: Double) :String {
// 1
val geocoder = Geocoder(this)
val Addresses: List?
val address: Address?
var addressText = ""
try {
// 2
addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1)
// 3
if (null != addresses && !addresses.isEmpty()) {
address = addresses[0]
val mFullAddress = Geocoder(this, Locale.getDefault())
var mUserCidade = ""
if (addresses[0].locality == null){ //as vezes a cidade vem em locality e outras em subAdminArea, então precisamos fazer essa verificação
mUserCidade = addresses[0].subAdminArea
} else {
mUserCidade = addresses[0].locality
}
val mUserEstado = addresses[0].adminArea
val mUserBairro = addresses[0].subLocality
val mUserNumeroCasa = addresses[0].subThoroughfare
val mUserRua = addresses[0].thoroughfare
val mUuserPais = addresses[0].countryName
val mUserCep = addresses[0].postalCode
addressText = mUserRua+" nº "+mUserNumeroCasa+", "+mUserBairro+", "+mUserCidade+" - "+mUserEstado
//user as outras variaveis se quiser usar cep, país, bairro
}
} catch (e: IOException) {
Log.e("MapsActivity", e.localizedMessage)
}
return addressText
}
Remarks: For this method to work you must already have the permissions of the user to access this information and also have already put in the manifest.
is not working
– Aline
wait...just one thing here
– Aline
I need to put this code on the oncreate is?
– Aline
No. You create this class only when you need it, if you need it.
– Reginaldo Rigo
You read the comments of the people on the original page of that reply?
– Reginaldo Rigo
Now it worked...but I did not create class no. I put in the oncreate pq in that method I receive the data from the webservice and Seto
– Aline
I will read friend. I hope I know how to look for my questions...the problem is that I do not know English :(
– Aline