Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added my location button toggle #46

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -617,12 +617,13 @@ enableAccessibilityElements(enabled: boolean) => Promise<void>
### enableCurrentLocation(...)

```typescript
enableCurrentLocation(enabled: boolean) => Promise<void>
enableCurrentLocation(enabled: boolean, options: EnableCurrentLocationOptions) => Promise<void>
```

| Param | Type |
| ------------- | -------------------- |
| **`enabled`** | <code>boolean</code> |
| Param | Type |
| ------------- | ------------------------------------------------------------------------------------- |
| **`enabled`** | <code>boolean</code> |
| **`options`** | <code><a href="#enablecurrentlocationoptions">EnableCurrentLocationOptions</a></code> |

--------------------

Expand Down Expand Up @@ -1030,6 +1031,13 @@ Configuration properties for a Google Map Camera
| **`animationDuration`** | <code>number</code> | This configuration option is not being used. | |


#### EnableCurrentLocationOptions

| Prop | Type |
| ------------------ | -------------------- |
| **`enableButton`** | <code>boolean</code> |


#### MapPadding

Controls for setting padding on the 'visible' region of the view.
Expand Down
16 changes: 12 additions & 4 deletions plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -618,12 +618,13 @@ enableAccessibilityElements(enabled: boolean) => Promise<void>
### enableCurrentLocation(...)

```typescript
enableCurrentLocation(enabled: boolean) => Promise<void>
enableCurrentLocation(enabled: boolean, options: EnableCurrentLocationOptions) => Promise<void>
```

| Param | Type |
| ------------- | -------------------- |
| **`enabled`** | <code>boolean</code> |
| Param | Type |
| ------------- | ------------------------------------------------------------------------------------- |
| **`enabled`** | <code>boolean</code> |
| **`options`** | <code><a href="#enablecurrentlocationoptions">EnableCurrentLocationOptions</a></code> |

--------------------

Expand Down Expand Up @@ -1044,6 +1045,13 @@ Configuration properties for a Google Map Camera
| **`animationDuration`** | <code>number</code> | This configuration option is not being used. | |


#### EnableCurrentLocationOptions

| Prop | Type |
| ------------------ | -------------------- |
| **`enableButton`** | <code>boolean</code> |


#### MapPadding

Controls for setting padding on the 'visible' region of the view.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CapacitorGoogleMap(
private val markers = HashMap<String, CapacitorGoogleMapMarker>()
private val polygons = HashMap<String, CapacitorGoogleMapsPolygon>()
private val circles = HashMap<String, CapacitorGoogleMapsCircle>()
private val polylines = HashMap<String, CapacitorGoogleMapPolyline>()
private val polylines = HashMap<String, CapacitorGoogleMapPolyline>()
private val markerIcons = HashMap<String, Bitmap>()
private var clusterManager: ClusterManager<CapacitorGoogleMapMarker>? = null

Expand Down Expand Up @@ -310,7 +310,7 @@ class CapacitorGoogleMap(
}
val googleMapPolyline = googleMap?.addPolyline(polylineOptions.await())
googleMapPolyline?.tag = it.tag

it.googleMapsPolyline = googleMapPolyline

polylines[googleMapPolyline!!.id] = it
Expand Down Expand Up @@ -636,11 +636,12 @@ class CapacitorGoogleMap(
}

@SuppressLint("MissingPermission")
fun enableCurrentLocation(enabled: Boolean, callback: (error: GoogleMapsError?) -> Unit) {
fun enableCurrentLocation(enabled: Boolean, enableButton: Boolean, callback: (error: GoogleMapsError?) -> Unit) {
try {
googleMap ?: throw GoogleMapNotAvailable()
CoroutineScope(Dispatchers.Main).launch {
googleMap?.isMyLocationEnabled = enabled
googleMap?.uiSettings?.isMyLocationButtonEnabled = enableButton
callback(null)
}
} catch (e: GoogleMapsError) {
Expand Down Expand Up @@ -739,7 +740,7 @@ class CapacitorGoogleMap(

return polygonOptions
}

private fun buildPolyline(line: CapacitorGoogleMapPolyline): PolylineOptions {
val polylineOptions = PolylineOptions()
polylineOptions.width(line.strokeWidth * this.config.devicePixelRatio)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,9 @@ class CapacitorGoogleMapsPlugin : Plugin(), OnMapsSdkInitializedCallback {
val enabled =
call.getBoolean("enabled") ?: throw InvalidArgumentsError("enabled is missing")

map.enableCurrentLocation(enabled) { err ->
val enableButton = call.getBoolean("enableButton") ?: false

map.enableCurrentLocation(enabled, enableButton) { err ->
if (err != null) {
throw err
}
Expand Down
4 changes: 3 additions & 1 deletion plugin/ios/Plugin/CapacitorGoogleMapsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,10 @@ public class CapacitorGoogleMapsPlugin: CAPPlugin, GMSMapViewDelegate {
if enabled && checkLocationPermission() != "granted" {
throw GoogleMapErrors.permissionsDeniedLocation
}

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

try map.enableCurrentLocation(enabled: enabled)
try map.enableCurrentLocation(enabled: enabled, enableButton: enableButton)

call.resolve()
} catch {
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, enableButton: Bool) throws {
DispatchQueue.main.sync {
self.mapViewController.GMapView.isMyLocationEnabled = enabled
self.mapViewController.GMapView.settings.myLocationButton = enableButton
}
}

Expand Down
4 changes: 4 additions & 0 deletions plugin/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,10 @@ export interface CircleClickCallbackData {
tag?: string;
}

export interface EnableCurrentLocationOptions {
enableButton?: boolean;
}

export interface MyLocationButtonClickCallbackData {
mapId: string;
}
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;
enableButton: boolean;
}
export interface AddMarkersArgs {
id: string;
Expand Down
9 changes: 6 additions & 3 deletions plugin/src/map.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Capacitor } from '@capacitor/core';
import type { PluginListenerHandle } from '@capacitor/core';

import type {
import {
CameraConfig,
Marker,
MapPadding,
Expand All @@ -19,6 +19,7 @@ import type {
CircleClickCallbackData,
Polyline,
PolylineCallbackData,
EnableCurrentLocationOptions,
} from './definitions';
import { LatLngBounds, MapType } from './definitions';
import type { CreateMapArgs } from './implementation';
Expand Down Expand Up @@ -55,7 +56,7 @@ export interface GoogleMapInterface {
enableIndoorMaps(enabled: boolean): Promise<void>;
enableTrafficLayer(enabled: boolean): Promise<void>;
enableAccessibilityElements(enabled: boolean): Promise<void>;
enableCurrentLocation(enabled: boolean): Promise<void>;
enableCurrentLocation(enabled: boolean, options: EnableCurrentLocationOptions): Promise<void>;
setPadding(padding: MapPadding): Promise<void>;
/**
* Get the map's current viewport latitude and longitude bounds.
Expand Down Expand Up @@ -532,12 +533,14 @@ export class GoogleMap {
* Set whether the My Location dot and accuracy circle is enabled.
*
* @param enabled
* @param options
* @returns
*/
async enableCurrentLocation(enabled: boolean): Promise<void> {
async enableCurrentLocation(enabled: boolean, options: EnableCurrentLocationOptions = {}): Promise<void> {
return CapacitorGoogleMaps.enableCurrentLocation({
id: this.id,
enabled,
enableButton: options.enableButton ?? false,
});
}

Expand Down