I am trying to add google maps to my app following this document - https://developers.google.com/maps/documentation/android-sdk/maps-compose
The problem is when I set is isMyLocationEnabled = true
I got this error, I want to display myLocation button, when it is to false it is working
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@[email protected] (040700-0):27)
at com.google.android.gms.maps.internal.i.a(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040700-0):138)
at cy.onTransact(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040700-0):4)
at android.os.Binder.transact(Binder.java:1043)
...
...
I got both permissions in my manifest file
<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>
And this is my 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'
And my code in compose scope
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
){
}
}
CodePudding user response:
you can make use of google's accompanist library to check for run time permission with jetpack compose
implementation "com.google.accompanist:accompanist-permissions:<version>"
Declare Permission State
val permissionState = rememberPermissionState(
permission = Manifest.permission.ACCESS_FINE_LOCATION,
)
On Tap of a button or a lifecycle event you can request run time permissions
Button(onClick = { state.launchPermissionRequest() }) {
Text("Request Permission")
}
Listen for the permission result from permissionState flow
if (permissionState.hasPermission) {
// do something
}