Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -636,18 +636,24 @@ class CapacitorGoogleMap(
}

@SuppressLint("MissingPermission")
fun enableCurrentLocation(enabled: Boolean, callback: (error: GoogleMapsError?) -> Unit) {
fun enableCurrentLocation(
showLocationDot: Boolean,
showMyLocationButton: Boolean,
callback: (error: GoogleMapsError?) -> Unit
) {
try {
googleMap ?: throw GoogleMapNotAvailable()
CoroutineScope(Dispatchers.Main).launch {
googleMap?.isMyLocationEnabled = enabled
googleMap?.isMyLocationEnabled = showLocationDot
googleMap?.uiSettings?.isMyLocationButtonEnabled = showMyLocationButton
callback(null)
}
} catch (e: GoogleMapsError) {
callback(e)
}
}


fun setPadding(padding: GoogleMapPadding, callback: (error: GoogleMapsError?) -> Unit) {
try {
googleMap ?: throw GoogleMapNotAvailable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,14 +763,34 @@ class CapacitorGoogleMapsPlugin : Plugin(), OnMapsSdkInitializedCallback {
}

@PluginMethod
fun enableCurrentLocation(call: PluginCall) {
if (getPermissionState(LOCATION) != PermissionState.GRANTED) {
requestAllPermissions(call, "enableCurrentLocationCallback")
} else {
internalEnableCurrentLocation(call)
@SuppressLint("MissingPermission")
fun internalEnableCurrentLocation(call: PluginCall) {
val id = call.getString("id")
val enabled = call.getBoolean("enabled") ?: false
val showButton = call.getBoolean("showButton") ?: false

if (id == null) {
call.reject("ID is required")
return
}

val map = maps[id]
if (map == null) {
call.reject("Map not found")
return
}

map.enableCurrentLocation(enabled) { error ->
if (error != null) {
call.reject("Error enabling location", error.localizedMessage)
} else {
map.setMyLocationButtonEnabled(showButton)
call.resolve()
}
}
}


@PermissionCallback
fun enableCurrentLocationCallback(call: PluginCall) {
if (getPermissionState(LOCATION) == PermissionState.GRANTED) {
Expand Down
8 changes: 6 additions & 2 deletions plugin/ios/Plugin/CapacitorGoogleMapsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ public class CapacitorGoogleMapsPlugin: CAPPlugin, GMSMapViewDelegate {
handleError(call, error: error)
}
}

@objc func enableCurrentLocation(_ call: CAPPluginCall) {
do {
guard let id = call.getString("id") else {
Expand All @@ -684,20 +684,24 @@ public class CapacitorGoogleMapsPlugin: CAPPlugin, GMSMapViewDelegate {
throw GoogleMapErrors.invalidArguments("enabled is missing")
}

let showButton = call.getBool("showButton") ?? false

let locationStatus = checkLocationPermission()

if enabled && !(locationStatus == "granted" || locationStatus == "prompt") {
if enabled && !(locationStatus == "granted" || locationStatus == "prompt") {
throw GoogleMapErrors.permissionsDeniedLocation
}

try map.enableCurrentLocation(enabled: enabled)
map.setMyLocationButtonEnabled(enabled: showButton)

call.resolve()
} catch {
handleError(call, error: error)
}
}


@objc func enableClustering(_ call: CAPPluginCall) {
do {
guard let id = call.getString("id") else {
Expand Down
3 changes: 2 additions & 1 deletion plugin/ios/Plugin/Map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,10 @@ public class Map {
}
}

func enableCurrentLocation(enabled: Bool) throws {
func enableCurrentLocation(enabled: Bool, showButton: Bool) throws {
DispatchQueue.main.sync {
self.mapViewController.GMapView.isMyLocationEnabled = enabled
self.mapViewController.GMapView.settings.myLocationButton = showButton
}
}

Expand Down
1 change: 1 addition & 0 deletions plugin/src/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export interface PaddingArgs {
export interface CurrentLocArgs {
id: string;
enabled: boolean;
showButton?: boolean;
}
export interface AddMarkersArgs {
id: string;
Expand Down