I have made a Location class myself with two Double val's (the longitude and latitude)
Now in an empty constructor I would like to use a FusedLocationProviderClient to get the device's current location which I then store in the two val's to represent the current location. I make one with LocationServices, then onSucces I put the variables inside the class. The problem is that I get the following error:
Captured member values initialization is forbidden due to possible reassignment
The constructor looks as following:
constructor(context: Context) {
val client = LocationServices.getFusedLocationProviderClient(context)
client.lastLocation.addOnSuccessListener { location: Location? ->
if (location != null) {
this.latitude = location.latitude
this.longitude = location.longitude
}
else {
this.latitude = 0.0
this.longitude = 0.0
}
}
}
I am a bit lost here. How do I work around this issue, so I have the user's coordinates in the variables?
Is there some sort of way for example, where I force the lastlocation to finish first, before storing the variables, so the compiler is sure it has a value which will not be reassigned as well?