Search code examples
kotlinandroid-jetpack-composeandroid-viewarcgis-android-api

How to change arcgis map for kotlin locationDisplay mode in jetpack compose?


I successfully integrated an ArcGIS MapView within a composable layout, and the map rendered flawlessly. Subsequently, I activated the location display mode, which functioned as expected.

    val mapView by lazy { MapView(context) }
    val locationDisplay = mapView.locationDisplay

    lifecycleScope.launch {
        // listen to changes in the status of the location data source
        locationDisplay.dataSource.start().onFailure {
            println(it.message)
        }
    }


  AndroidView(
    modifier = Modifier.fillMaxSize(),
    factory = { context ->
        mapView.also { mapView ->
            mapView.apply {
                locationDisplay.setAutoPanMode(LocationDisplayAutoPanMode.CompassNavigation)
            }
        }
    },
    update = { view -> // view is automatically cast to a MapView
        ...
    }
)

Here is my problem:

I want to change Arcgis locationDisplay mode by a button click, so I created a button outside the AndroidView component, writed a clickable function to set the locationDisplay mode to LocationDisplayAutoPanMode.Recenter, but it did not work as I expected!


Solution

  • the localtionDisplay should be remembered in the compose, and the value could be stored in the composition during the initial composition and the stored value is returned during recomposition. Here is my adjusted code:

     val mapView by lazy { MapView(context) }
     val locationDisplay = remember {mapView.locationDisplay}
    
        lifecycleScope.launch {
            // listen to changes in the status of the location data source
            locationDisplay.dataSource.start().onFailure {
                println(it.message)
            }
        }
    
    
      AndroidView(
        modifier = Modifier.fillMaxSize(),
        factory = { context ->
            mapView.also { mapView ->
                mapView.apply {
                    locationDisplay.setAutoPanMode(LocationDisplayAutoPanMode.CompassNavigation)
                }
            }
        },
        update = { view -> // view is automatically cast to a MapView
            ...
        }
    )