Travis Griggs
07/18/2023, 5:56 PMscope.launch {
val locationClient =
LocationServices.getFusedLocationProviderClient(context)
val location = await(locationClient.getCurrentLocation(PRIORITY_HIGH_ACCURACY, null))
val target = LatLng(location.latitude, location.longitude)
var update = CameraUpdateFactory.newLatLng(target)
cameraState.animate(update, 300)
}
But I get an error ( java.lang.IllegalStateException: Must not be called on the main application thread) when I run this. I guess I thought the scope.launch would take care of that. I'm missing a bit of the magic with tasks/coroutines I think. What do I need to add/change to make that work?Chrimaeon
07/18/2023, 6:08 PMscope
Travis Griggs
07/18/2023, 6:11 PMChrimaeon
07/18/2023, 6:16 PMlaunch(<http://Dispatchers.IO|Dispatchers.IO>)
or use a withContext(<http://Dispatchers.IO|Dispatchers.IO>)
within the scope to switch the context to a background thread.ephemient
07/18/2023, 6:24 PMrememberCoroutineScope { <http://Dispatchers.IO|Dispatchers.IO> }