I am trying to make a function that returns the longitude and latitude in the form of a string. I cannot for the life of me figure out why:
- the lambda function refuses to update the value please to the actual coordinates (the function does actually manage to get the location because if i uncomment that line with Log it does actually post some coords)
- Avoid the lambda function altogether if possible (problem 1 being solved has priority over this)
fun getLatLongString(location: Location?): String {
val latitude = location?.latitude
val longitude = location?.longitude
return "Latitude: $latitude, Longitude: $longitude"
}
// @SuppressLint("MissingPermission")
fun getLocation():String {
var please = "no location"
val task: Task<Location> = fusedLocationClient.lastLocation
task.addOnSuccessListener { location: Location? -> String
if (location != null) {
var latLongString:String = getLatLongString(location)
please = latLongString
//Log.i("I", "Location $please")
}else{
please = "location not working"
Log.i("I", "Location not found")
}
}
return please
}
these are my 2 functions. The first one getLatLongString
simply formats and transforms the data and the second function getLocation
is the main function that handles the gathering of location.
Any help is greatly appreciated!!
P.S. there is a error on the line where the task is declared but that error is related to missing permissions which i handled somewhere else.