Jason Inbody
07/19/2021, 11:44 PMjava.lang.IllegalStateException: pending composition has not been applied
imply? I recently copy and pasted some google map code and now I'm getting this errorJason Inbody
07/19/2021, 11:46 PMpackage com.example.myapplication.view
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.viewinterop.AndroidView
import com.example.myapplication.components.rememberMapViewWithLifecycle
import com.example.myapplication.utility.FinUser
import com.google.android.libraries.maps.CameraUpdateFactory
import com.google.android.libraries.maps.model.LatLng
import com.google.maps.android.ktx.awaitMap
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@Composable
fun MapScreen(user: FinUser) {
val mapView = rememberMapViewWithLifecycle()
Column(
modifier = Modifier
.fillMaxHeight()
.fillMaxWidth()
.background(Color.White)
) {
AndroidView({ mapView}) {mapView->
CoroutineScope(Dispatchers.Main).launch {
val map = mapView.awaitMap()
map.uiSettings.isZoomControlsEnabled = true
val destination = LatLng(-32.491, 147.309)
map.moveCamera(CameraUpdateFactory.newLatLngZoom(destination,6f))
}
}
}
}
Jason Inbody
07/19/2021, 11:46 PMpackage com.example.myapplication.components
import android.os.Bundle
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import com.example.myapplication.R
import com.google.android.libraries.maps.MapView
@Composable
fun rememberMapViewWithLifecycle(): MapView {
val context = LocalContext.current
val mapView = remember {
MapView(context).apply {
id = R.id.map
}
}
// Makes MapView follow the lifecycle of this composable
val lifecycleObserver = rememberMapLifecycleObserver(mapView)
val lifecycle = LocalLifecycleOwner.current.lifecycle
DisposableEffect(lifecycle) {
lifecycle.addObserver(lifecycleObserver)
onDispose {
lifecycle.removeObserver(lifecycleObserver)
}
}
return mapView
}
@Composable
fun rememberMapLifecycleObserver(mapView: MapView):
LifecycleEventObserver = remember(mapView) {
LifecycleEventObserver { _, event ->
when (event) {
Lifecycle.Event.ON_CREATE -> mapView.onCreate(Bundle())
Lifecycle.Event.ON_START -> mapView.onStart()
Lifecycle.Event.ON_RESUME -> mapView.onResume()
Lifecycle.Event.ON_PAUSE -> mapView.onPause()
Lifecycle.Event.ON_STOP -> mapView.onStop()
Lifecycle.Event.ON_DESTROY -> mapView.onDestroy()
else -> throw IllegalStateException()
}
}
}
Jason Inbody
07/19/2021, 11:47 PMJason Inbody
07/20/2021, 12:43 AM<meta-data android:name="com.google.android.geo.<i also placed my api key here like an idiot>"
android:value="<my api ket>"/>
should be
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="<my api ket>"/>