Skip to content

Commit

Permalink
feat(google-maps): Added feature to disable touch events on map
Browse files Browse the repository at this point in the history
  • Loading branch information
A1Jan committed May 23, 2023
1 parent 5e53d83 commit 908b003
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 0 deletions.
20 changes: 20 additions & 0 deletions google-maps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ export default MyMap;
<docgen-index>

* [`create(...)`](#create)
* [`enableTouch()`](#enabletouch)
* [`disableTouch()`](#disabletouch)
* [`enableClustering(...)`](#enableclustering)
* [`disableClustering()`](#disableclustering)
* [`addMarker(...)`](#addmarker)
Expand Down Expand Up @@ -338,6 +340,24 @@ create(options: CreateMapArgs, callback?: MapListenerCallback<MapReadyCallbackDa
--------------------


### enableTouch()

```typescript
enableTouch() => Promise<void>
```

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


### disableTouch()

```typescript
disableTouch() => Promise<void>
```

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


### enableClustering(...)

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class CapacitorGoogleMapsPlugin : Plugin() {
private var maps: HashMap<String, CapacitorGoogleMap> = HashMap()
private var cachedTouchEvents: HashMap<String, MutableList<MotionEvent>> = HashMap()
private val tag: String = "CAP-GOOGLE-MAPS"
private var touchEnabled: HashMap<String, Boolean> = HashMap()

companion object {
const val LOCATION = "location"
Expand All @@ -56,6 +57,9 @@ class CapacitorGoogleMapsPlugin : Plugin() {
val touchY = event.y

for ((id, map) in maps) {
if (touchEnabled[id] == false) {
continue
}
val mapRect = map.getMapBounds()
if (mapRect.contains(touchX.toInt(), touchY.toInt())) {
if (event.action == MotionEvent.ACTION_DOWN) {
Expand Down Expand Up @@ -169,6 +173,34 @@ class CapacitorGoogleMapsPlugin : Plugin() {
}
}

@PluginMethod
fun enableTouch(call: PluginCall) {
try {
val id = call.getString("id")
id ?: throw InvalidMapIdError()
touchEnabled[id] = true
call.resolve()
} catch (e: GoogleMapsError) {
handleError(call, e)
} catch (e: Exception) {
handleError(call, e)
}
}

@PluginMethod
fun disableTouch(call: PluginCall) {
try {
val id = call.getString("id")
id ?: throw InvalidMapIdError()
touchEnabled[id] = false
call.resolve()
} catch (e: GoogleMapsError) {
handleError(call, e)
} catch (e: Exception) {
handleError(call, e)
}
}

@PluginMethod
fun addMarker(call: PluginCall) {
try {
Expand Down
2 changes: 2 additions & 0 deletions google-maps/ios/Plugin/CapacitorGoogleMapsPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// each method the plugin supports using the CAP_PLUGIN_METHOD macro.
CAP_PLUGIN(CapacitorGoogleMapsPlugin, "CapacitorGoogleMaps",
CAP_PLUGIN_METHOD(create, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(enableTouch, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(disableTouch, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(addMarker, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(addMarkers, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(addPolygons, CAPPluginReturnPromise);
Expand Down
36 changes: 36 additions & 0 deletions google-maps/ios/Plugin/CapacitorGoogleMapsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,42 @@ public class CapacitorGoogleMapsPlugin: CAPPlugin, GMSMapViewDelegate {
}
}

@objc func enableTouch(_ call: CAPPluginCall) {
do {
guard let id = call.getString("id") else {
throw GoogleMapErrors.invalidMapId
}

guard let map = self.maps[id] else {
throw GoogleMapErrors.mapNotFound
}

map.enableTouch()

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

@objc func disableTouch(_ call: CAPPluginCall) {
do {
guard let id = call.getString("id") else {
throw GoogleMapErrors.invalidMapId
}

guard let map = self.maps[id] else {
throw GoogleMapErrors.mapNotFound
}

map.disableTouch()

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

@objc func addMarker(_ call: CAPPluginCall) {
do {
guard let id = call.getString("id") else {
Expand Down
22 changes: 22 additions & 0 deletions google-maps/ios/Plugin/Map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,25 @@ public class Map {

func destroy() {
DispatchQueue.main.async {
self.enableTouch()
self.targetViewController?.tag = 0
self.mapViewController.view = nil
}
}

func enableTouch() {
DispatchQueue.main.async {
if let target = self.targetViewController, let itemIndex = WKWebView.disabledTargets.firstIndex(of: target) {
WKWebView.disabledTargets.remove(at: itemIndex)
}
}
}

func disableTouch() {
DispatchQueue.main.async {
if let target = self.targetViewController, !WKWebView.disabledTargets.contains(target) {
WKWebView.disabledTargets.append(target)
}
}
}

Expand Down Expand Up @@ -617,9 +633,15 @@ private func getResizedIcon(_ iconImage: UIImage, _ marker: Marker) -> UIImage?
}

extension WKWebView {
static var disabledTargets: [UIView] = []

override open func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
var hitView = super.hitTest(point, with: event)

if let tempHitView = hitView, WKWebView.disabledTargets.contains(tempHitView) {
return nil
}

if let typeClass = NSClassFromString("WKChildScrollView"), let tempHitView = hitView, tempHitView.isKind(of: typeClass) {
for item in tempHitView.subviews.reversed() {
let convertPoint = item.convert(point, from: self)
Expand Down
2 changes: 2 additions & 0 deletions google-maps/src/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ export interface EnableClusteringArgs {

export interface CapacitorGoogleMapsPlugin extends Plugin {
create(options: CreateMapArgs): Promise<void>;
enableTouch(args: { id: string }): Promise<void>;
disableTouch(args: { id: string }): Promise<void>;
addMarker(args: AddMarkerArgs): Promise<{ id: string }>;
addMarkers(args: AddMarkersArgs): Promise<{ ids: string[] }>;
removeMarker(args: RemoveMarkerArgs): Promise<void>;
Expand Down
24 changes: 24 additions & 0 deletions google-maps/src/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export interface GoogleMapInterface {
options: CreateMapArgs,
callback?: MapListenerCallback<MapReadyCallbackData>,
): Promise<GoogleMap>;
enableTouch(): Promise<void>;
disableTouch(): Promise<void>;
enableClustering(
/**
* The minimum number of markers that can be clustered together. The default is 4 markers.
Expand Down Expand Up @@ -233,6 +235,28 @@ export class GoogleMap {
});
}

/**
* Enable touch events on native map
*
* @returns void
*/
async enableTouch(): Promise<void> {
return CapacitorGoogleMaps.enableTouch({
id: this.id,
});
}

/**
* Disable touch events on native map
*
* @returns void
*/
async disableTouch(): Promise<void> {
return CapacitorGoogleMaps.disableTouch({
id: this.id,
});
}

/**
* Enable marker clustering
*
Expand Down
8 changes: 8 additions & 0 deletions google-maps/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ export class CapacitorGoogleMapsWeb
}
}

async enableTouch(_args: { id: string }): Promise<void> {

Check warning on line 141 in google-maps/src/web.ts

View workflow job for this annotation

GitHub Actions / lint

'_args' is defined but never used
throw new Error('Method not supported on web.');
}

async disableTouch(_args: { id: string }): Promise<void> {

Check warning on line 145 in google-maps/src/web.ts

View workflow job for this annotation

GitHub Actions / lint

'_args' is defined but never used
throw new Error('Method not supported on web.');
}

async setCamera(_args: CameraArgs): Promise<void> {
// Animation not supported yet...
this.maps[_args.id].map.moveCamera({
Expand Down

0 comments on commit 908b003

Please sign in to comment.