Android Jetpack Compose: 使用谷歌地图在创建地图时崩溃

5
我正在尝试使用 Google Compose 示例项目 Crane 实现 Google 地图,详情请见以下链接:https://github.com/android/compose-samples/tree/main/Crane
我采用了相同的实现方式,并使用 MapViewUtils 来实现地图的生命周期管理、防止重组合等操作。我将所有 Android 地图密钥和权限都放在 manifest 中。但是,在地图启动时我的代码会崩溃:
这是我想要展示地图的部分:
@Composable
fun MapScreen(latitude: String, longitude: String) {
    // The MapView lifecycle is handled by this composable. As the MapView also needs to be updated
    // with input from Compose UI, those updates are encapsulated into the MapViewContainer
    // composable. In this way, when an update to the MapView happens, this composable won't
    // recompose and the MapView won't need to be recreated.
    val mapView = rememberMapViewWithLifecycle()
    MapViewContainer(mapView, latitude, longitude)
}

@Composable
private fun MapViewContainer(
    map: MapView,
    latitude: String,
    longitude: String
) {
//    var zoom by savedInstanceState { InitialZoom }

    AndroidView({ map }) { mapView ->
        // Reading zoom so that AndroidView recomposes when it changes. The getMapAsync lambda
        mapView.getMapAsync {
            val position = LatLng(latitude.toDouble(), longitude.toDouble())
            it.addMarker(
                MarkerOptions().position(position)
            )
            it.moveCamera(CameraUpdateFactory.newLatLng(position))
        }
    }
}

以下是 Util 类中的内容:

@Composable
fun rememberMapViewWithLifecycle(): MapView {
    val context = ContextAmbient.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 = LifecycleOwnerAmbient.current.lifecycle
    onCommit(lifecycle) {
        lifecycle.addObserver(lifecycleObserver)
        onDispose {
            lifecycle.removeObserver(lifecycleObserver)
        }
    }

    return mapView
}

@Composable
private fun rememberMapLifecycleObserver(mapView: MapView): LifecycleEventObserver =
    remember(mapView) {
        LifecycleEventObserver { _, event ->
            when (event) {
                Lifecycle.Event.ON_CREATE -> mapView.onCreate(Bundle()) //Crashes here
                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()
            }
        }
    }

我遇到了这个崩溃:

2020-11-05 12:16:09.282 2665-3383/com.google.android.gms.persistent E/ModuleIdSetter: exception when setting module id
    java.lang.IllegalStateException: Unable to get current module info in ModuleManager created with non-module Context
        at com.google.android.chimera.config.ModuleManager.getCurrentModule(:com.google.android.gms@202414022@20.24.14 (040700-319035315):2)
        at aewd.a(:com.google.android.gms@202414022@20.24.14 (040700-319035315):4)
        at aewg.b(:com.google.android.gms@202414022@20.24.14 (040700-319035315):9)
        at aeso.a(Unknown Source:0)
        at rpm.a(:com.google.android.gms@202414022@20.24.14 (040700-319035315):0)
        at rlv.c(:com.google.android.gms@202414022@20.24.14 (040700-319035315):1)
        at rlt.b(:com.google.android.gms@202414022@20.24.14 (040700-319035315):1)
        at rok.b(:com.google.android.gms@202414022@20.24.14 (040700-319035315):6)
        at rok.c(:com.google.android.gms@202414022@20.24.14 (040700-319035315):6)
        at rok.b(:com.google.android.gms@202414022@20.24.14 (040700-319035315):10)
        at rok.a(:com.google.android.gms@202414022@20.24.14 (040700-319035315):17)
        at rok.g(:com.google.android.gms@202414022@20.24.14 (040700-319035315):3)
        at sdr.a(:com.google.android.gms@202414022@20.24.14 (040700-319035315):2)
        at scr.a(:com.google.android.gms@202414022@20.24.14 (040700-319035315):10)
        at sci.a(:com.google.android.gms@202414022@20.24.14 (040700-319035315):0)
        at scl.handleMessage(:com.google.android.gms@202414022@20.24.14 (040700-319035315):28)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at aekz.a(:com.google.android.gms@202414022@20.24.14 (040700-319035315):2)
        at aekz.dispatchMessage(:com.google.android.gms@202414022@20.24.14 (040700-319035315):14)
        at android.os.Looper.loop(Looper.java:214)
        at android.os.HandlerThread.run(HandlerThread.java:67)
2个回答

1

您需要请求许可以访问用户的位置,并确保在显示地图之前您已经获得了它。您可以使用带有LiveDataViewModel的变量,在许可被授予时进行更新,这是示例的一部分:

class MainViewModel : ViewModel() {
    private val _permissionGranted = MutableLiveData(false)
    val permissionGranted = _permissionGranted
    
    fun onPermissionGranted() = _permissionGranted.postValue(true)
    
    // ...
}

class MainActivity : AppCompatActivity() {
    private val mainViewModel by viewModels<MainViewModel>
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val permissionGranted = mainViewModel.permissionGranted.observeAsState()
            if (permissionGranted) {
                // logic to show your map
            } else {
                // logic to ask for permission
            }
        }
    }
    
    override fun onRequestPermissionsResult(requestCode: Int,
        permissions: Array<String>, grantResults: IntArray) {
        // check if it's your request
        mainViewModel.onPremissionGranted()
    }
    
    // ...
}

您可以在此处获取有关请求权限的更多信息:https://developer.android.com/training/permissions/requesting


正如我所提到的,我已处理了所有权限。崩溃与权限无关。 - Mahdi
你说你在清单文件中添加了权限,但是你有请求该权限吗? - Vitor Ramos
位置权限不属于危险权限,因此无需单独请求。将其放入清单文件中即可。 - Mahdi
根据文档,你必须请求位置权限:https://developer.android.com/training/location/permissions - Vitor Ramos
好的,你说得对,我没有询问用户位置,只是想在地图上显示一些东西,所以它可以工作。我想这就是原因。顺便说一下,我现在可以在我的应用程序中使用旧版地图而不需要应用内权限,并且它运行良好。嗯,这可能与我使用com.google.android.gsm.map的旧方式有关,但在那个例子中,他们使用lcom.google.android.libraries.maps?所以在第二个示例中总是需要请求应用内权限吗? - Mahdi
这与此无关,如果您尝试获取用户位置,则需要在Android 6或更高版本上请求权限。 - Vitor Ramos

-1

将 "fun rememberMapViewWithLifecycle(): MapView" 中的返回类型去掉,修改为 "fun rememberMapViewWithLifecycle()"。

很明显,您的组合函数正在返回一个 MapView,但是为了使其被认为是组合函数,它不应该返回任何类型。 我引用自“https://developer.android.com/jetpack/compose/mental-model"

""" 该函数不返回任何内容。发出 UI 的 Compose 函数不需要返回任何内容,因为它们描述所需的屏幕状态而不是构造 UI 小部件。

"""


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接