diff --git a/plugin/README.md b/plugin/README.md index 0a79e8fb..f6a6231a 100644 --- a/plugin/README.md +++ b/plugin/README.md @@ -60,13 +60,27 @@ The Google Maps SDK for Android requires you to add your API key to the AndroidM ``` -To use certain location features, the SDK requires the following permissions to also be added to your AndroidManifest.xml: +To show the user's current location on the map (`enableCurrentLocation`), the app must declare at least one location permission in its AndroidManifest.xml. The plugin does not declare any location permission itself. + +`ACCESS_COARSE_LOCATION` is enough to show the current location on the map: + +```xml + +``` + +Add `ACCESS_FINE_LOCATION` as well only if your app needs precise location: ```xml ``` +The plugin requests the location permissions your app declares (like `@capacitor/geolocation`), but only needs coarse location to be granted to show the current location. If only `ACCESS_COARSE_LOCATION` is declared, only coarse location is requested. If `ACCESS_FINE_LOCATION` is also declared, it is requested too, even if coarse location was already granted; on Android 12 and above, if the user grants approximate location only, the current location is still shown with approximate accuracy. + +> **Google Play policy:** `ACCESS_FINE_LOCATION` is considered a sensitive permission. Apps that declare it must submit a declaration in the Play Console justifying why precise location is needed. If your app doesn't strictly need precise location, declare only `ACCESS_COARSE_LOCATION`. See [Google Play's location permissions policy](https://support.google.com/googleplay/android-developer/answer/16909972#location-permissions). + +If neither permission is declared, `enableCurrentLocation` rejects with a "location permission was denied" error. + ### Variables This plugin will use the following project variables (defined in your app's `variables.gradle` file): diff --git a/plugin/android/src/main/java/com/capacitorjs/plugins/googlemaps/CapacitorGoogleMapsPlugin.kt b/plugin/android/src/main/java/com/capacitorjs/plugins/googlemaps/CapacitorGoogleMapsPlugin.kt index fa2d698d..65b170f1 100644 --- a/plugin/android/src/main/java/com/capacitorjs/plugins/googlemaps/CapacitorGoogleMapsPlugin.kt +++ b/plugin/android/src/main/java/com/capacitorjs/plugins/googlemaps/CapacitorGoogleMapsPlugin.kt @@ -10,6 +10,7 @@ import com.getcapacitor.* import com.getcapacitor.annotation.CapacitorPlugin import com.getcapacitor.annotation.Permission import com.getcapacitor.annotation.PermissionCallback +import com.getcapacitor.util.PermissionHelper import com.google.android.gms.maps.MapsInitializer import com.google.android.gms.maps.OnMapsSdkInitializedCallback import com.google.android.gms.maps.model.LatLng @@ -25,9 +26,17 @@ import org.json.JSONObject permissions = [ Permission( - strings = [Manifest.permission.ACCESS_FINE_LOCATION], + strings = + [ + Manifest.permission.ACCESS_COARSE_LOCATION, + Manifest.permission.ACCESS_FINE_LOCATION + ], alias = CapacitorGoogleMapsPlugin.LOCATION ), + Permission( + strings = [Manifest.permission.ACCESS_COARSE_LOCATION], + alias = CapacitorGoogleMapsPlugin.COARSE_LOCATION + ), ], ) class CapacitorGoogleMapsPlugin : Plugin(), OnMapsSdkInitializedCallback { @@ -38,6 +47,7 @@ class CapacitorGoogleMapsPlugin : Plugin(), OnMapsSdkInitializedCallback { companion object { const val LOCATION = "location" + const val COARSE_LOCATION = "coarseLocation" } @SuppressLint("ClickableViewAccessibility") @@ -817,22 +827,52 @@ class CapacitorGoogleMapsPlugin : Plugin(), OnMapsSdkInitializedCallback { @PluginMethod fun enableCurrentLocation(call: PluginCall) { - if (getPermissionState(LOCATION) != PermissionState.GRANTED) { - requestAllPermissions(call, "enableCurrentLocationCallback") - } else { - internalEnableCurrentLocation(call) + val coarseDeclared = + PermissionHelper.hasDefinedPermission( + context, + Manifest.permission.ACCESS_COARSE_LOCATION + ) + val fineDeclared = + PermissionHelper.hasDefinedPermission( + context, + Manifest.permission.ACCESS_FINE_LOCATION + ) + + if (!coarseDeclared && !fineDeclared) { + call.reject( + "location permission was denied: neither ACCESS_COARSE_LOCATION nor ACCESS_FINE_LOCATION is declared in the AndroidManifest.xml" + ) + return } + + // Request every location permission the app declared (like @capacitor/geolocation), even if + // coarse is already granted. If ACCESS_FINE_LOCATION is declared and not granted yet, the + // `location` alias (coarse + fine) is requested; otherwise only coarse is requested. + val alias = if (fineDeclared) LOCATION else COARSE_LOCATION + if (getPermissionState(alias) != PermissionState.GRANTED) { + requestPermissionForAlias(alias, call, "enableCurrentLocationCallback") + return + } + + internalEnableCurrentLocation(call) } @PermissionCallback fun enableCurrentLocationCallback(call: PluginCall) { - if (getPermissionState(LOCATION) == PermissionState.GRANTED) { + // Google Maps only needs ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION to show the + // current location, and precise location also grants coarse. So coarse being granted is + // enough, even if the user declined (or kept approximate instead of) precise location. + if (hasLocationPermission()) { internalEnableCurrentLocation(call) } else { call.reject("location permission was denied") } } + private fun hasLocationPermission(): Boolean { + return getPermissionState(COARSE_LOCATION) == PermissionState.GRANTED + } + @PluginMethod fun setPadding(call: PluginCall) { try {