1、先来一个官方代码,这段代码是官方下载的代码,但是都是在activity中使用的,没有任何问题
@Composable
fun ComposeMapView(
modifier: Modifier = Modifier,
mapViewModel: MapViewModel
) {
// get an instance of the current lifecycle owner
val lifecycleOwner = LocalLifecycleOwner.current
// get an instance of the MapView state
val mapViewState = mapViewModel.mapViewState
// create and add MapView to the activity lifecycle
val mapView = createMapViewInstance(lifecycleOwner)
// wrap the MapView as an AndroidView
AndroidView(
modifier = modifier,
factory = { mapView },
// recomposes the MapView on changes in the MapViewState
update = { mapView ->
mapView.apply {
map = mapViewState.arcGISMap
setViewpoint(mapViewState.viewpoint)
// set the MapView's interaction options using the MapViewState
mapViewState.interactionOptions.apply {
interactionOptions.isMagnifierEnabled = isMagnifierEnabled
interactionOptions.allowMagnifierToPan = allowMagnifierToPan
}
}
}
)
}
/**
* Create the MapView instance and add it to the Activity lifecycle
*/
@Composable
fun createMapViewInstance(lifecycleOwner: LifecycleOwner): MapView {
// create the MapView
val mapView = MapView(LocalContext.current)
// add the side effects for MapView composition
DisposableEffect(lifecycleOwner) {
lifecycleOwner.lifecycle.addObserver(mapView)
onDispose {
lifecycleOwner.lifecycle.removeObserver(mapView)
}
}
return mapView
}
2、compose路由导航,分别为地图、任务、个人中心
NavHost(modifier = modifier, navController = navController, startDestination = "map", builder = {
navigation(startDestination = "map/index", route = "map") {
composable(
route = "map/index",
deepLinks = emptyList(),
content = { ArcgisMap() }
)
}
navigation(startDestination = "task/index", route = "task") {
composable(
route = "task/index",
deepLinks = emptyList(),
content = { TaskPage() }
)
}
navigation(startDestination = "mine/index", route = "mine") {
composable(
route = "mine/index",
deepLinks = emptyList(),
content = { MinePage() }
)
}
})
3、根据需求,需要添加图层 GraphicsOverlay,这时候切换到任务或者个人中心,再切换回来就会报错,arcgis # Object is already owned.: Already owned errcode:24。请问这种如何跳过重组呢,不可能地图每次都需要重新加载吧。想要获取到navigation里fragment的样式该如何去做
AndroidView(
modifier = modifier,
factory = { mapView },
// recomposes the MapView on changes in the MapViewState
update = { mapView ->
mapView.apply {
map = mapViewState.arcGISMap
setViewpoint(mapViewState.viewpoint)
// set the MapView's interaction options using the MapViewState
mapViewState.interactionOptions.apply {
interactionOptions.isMagnifierEnabled = isMagnifierEnabled
interactionOptions.allowMagnifierToPan = allowMagnifierToPan
}
graphicsOverlays.add(GraphicsOverlay().let {
val point = Point(103.111119, 31.055449, SpatialReference.wgs84())
val pointSymbol = PictureMarkerSymbol.createWithImage(
BitmapFactory.decodeResource(resources, R.mipmap.nnn).toDrawable(resources)
)
val pointRender = SimpleRenderer(pointSymbol)
it.graphics.add(Graphic(point))
it.renderer = pointRender
})//添加这句代码
}
}
)