获取当前位置按钮未显示在 jetpack compose 谷歌地图上

Get current location button is not show on the jetpack compose google map

提问人:Saeed Noshadi 提问时间:11/12/2021 最后编辑:Phil DukhovSaeed Noshadi 更新时间:6/7/2022 访问量:1450

问:

我想在 Google 地图上显示当前位置按钮 (),但它没有显示。isMyLocationButtonEnabled

如何显示“获取当前位置”按钮?

AndroidView(
    factory = { mapView }
) {
    mapView.getMapAsync { map ->
        map.apply {
            navigatorViewModel.apply {
                viewModelScope.launch {
                    isMapEditable.collectLatest {
                        uiSettings.setAllGesturesEnabled(
                            it,
                        )
                        uiSettings.isMyLocationButtonEnabled = true
                    }
                }
                val location = lastSelectedLocation.value
                val position = LatLng(location.latitude, location.longitude)
                moveCamera(
                    CameraUpdateFactory.newLatLngZoom(
                        position,
                        Constants.ZOOM_CAMERA
                    )
                )

                setOnCameraIdleListener {
                    val cameraPosition = map.cameraPosition
                    updateLocation(
                        cameraPosition.target.latitude,
                        cameraPosition.target.longitude
                    )
                }
            }
        }
    }
}
Kotlin 谷歌地图 Android-jetpack-compose

评论


答:

1赞 Kunal Sharma 3/5/2022 #1

我这么晚才回复,这个答案可能会对其他人有所帮助!

要在谷歌地图中显示 MyLocationButton,您需要同时启用两者map.isMyLocationEnabled = true & uiSettings.isMyLocationButtonEnabled = true

8赞 Sadegh.t 6/7/2022 #2

添加以下属性:

val uiSettings = remember {
    MapUiSettings(myLocationButtonEnabled = true)
}
val properties by remember {
    mutableStateOf(MapProperties(isMyLocationEnabled = true))
}

谷歌地图:

 GoogleMap(
    modifier = modifier.fillMaxSize(),
    cameraPositionState = cameraPositionState,
    properties = properties,
    uiSettings = uiSettings)