Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,27 @@ The Google Maps SDK for Android requires you to add your API key to the AndroidM
<meta-data android:name="com.google.android.geo.API_KEY" android:value="YOUR_API_KEY_HERE"/>
```

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
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
```

Add `ACCESS_FINE_LOCATION` as well only if your app needs precise location:

```xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
```

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):
Expand Down

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no breaking changes here, so this could technically go in main branch.

But because the main goal here is for Google Play policies that are only in effect for API level 37 (Capacitor 9), I don't mind this staying in next.

@alexgerardojacinto alexgerardojacinto Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main goal here is for Google Play policies that are only in effect for API level 37 (Capacitor 9)

Yeah that was my reasoning. I'll keep it in next and if anything if we for some reason want it in main (or Capacitor 8 once it's in 8.x) in the future we can always cherry pick

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -38,6 +47,7 @@ class CapacitorGoogleMapsPlugin : Plugin(), OnMapsSdkInitializedCallback {

companion object {
const val LOCATION = "location"
const val COARSE_LOCATION = "coarseLocation"
}

@SuppressLint("ClickableViewAccessibility")
Expand Down Expand Up @@ -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 {
Expand Down
Loading