Google jetpack compose 库导致地图崩溃

Google map crash with jetpack compose library

我正在尝试按照此文档将 google 地图添加到我的应用程序 - https://developers.google.com/maps/documentation/android-sdk/maps-compose

问题是当我设置为 isMyLocationEnabled = true 我收到这个错误,我想显示 myLocation 按钮,当它为 false 时它正在工作

    Process: com.example.tryingtofixgooglemap, PID: 12872
    java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
        at com.google.maps.api.android.lib6.impl.bi.c(:com.google.android.gms.dynamite_mapsdynamite@201817052@20.18.17 (040700-0):27)
        at com.google.android.gms.maps.internal.i.a(:com.google.android.gms.dynamite_mapsdynamite@201817052@20.18.17 (040700-0):138)
        at cy.onTransact(:com.google.android.gms.dynamite_mapsdynamite@201817052@20.18.17 (040700-0):4)
        at android.os.Binder.transact(Binder.java:1043)
...
...

我在清单文件中获得了两个权限

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tryingtofixgooglemap">
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.TryingToFixGoogleMap">

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="{api_key}" />
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.TryingToFixGoogleMap">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest> 

这是我的 build.gradle

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
    implementation "com.google.maps.android:maps-compose:1.0.0"
    implementation "com.google.android.gms:play-services-maps:18.0.2"
    implementation 'com.google.android.gms:play-services-location:19.0.1'

我的代码在 compose 范围内

var uiSettings by remember { mutableStateOf(
                            MapUiSettings(
                                zoomControlsEnabled = false,
                            )
                        )
                    }
                    var properties by remember { mutableStateOf(
                            MapProperties(
                            isMyLocationEnabled = true
                            )
                        )
                    }
                    Box(Modifier.fillMaxSize()) {
                        GoogleMap(
                            modifier = Modifier.matchParentSize(),
                            properties = properties,
                            uiSettings = uiSettings
                        ){

                        }


                    }

您可以使用 google 的伴奏库来检查 运行 jetpack compose 的时间许可

implementation "com.google.accompanist:accompanist-permissions:<version>"

声明权限状态

val permissionState = rememberPermissionState(
    permission = Manifest.permission.ACCESS_FINE_LOCATION,
)

点击按钮或生命周期事件,您可以请求 运行 时间权限

Button(onClick = { state.launchPermissionRequest() }) {
    Text("Request Permission")
}

监听permissionState流的权限结果

if (permissionState.hasPermission) {
    // do something
}

参考:https://google.github.io/accompanist/permissions/