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

feat(google-maps): Added feature to disable touch events on map #1601

Merged
merged 6 commits into from
Aug 9, 2023
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
20 changes: 20 additions & 0 deletions google-maps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ export default MyMap;
<docgen-index>

* [`create(...)`](#create)
* [`enableTouch()`](#enabletouch)
* [`disableTouch()`](#disabletouch)
* [`enableClustering(...)`](#enableclustering)
* [`disableClustering()`](#disableclustering)
* [`addMarker(...)`](#addmarker)
Expand Down Expand Up @@ -346,6 +348,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
34 changes: 34 additions & 0 deletions google-maps/e2e-tests/src/pages/Map/CreateAndDestroyMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,34 @@ const CreateAndDestroyMapPage: React.FC = () => {
}
}

async function disableMapTouchEvents() {
setCommandOutput('');

try {
if (maps) {
for (let map of maps) {
map.disableTouch();
}
}
} catch (err: any) {
setCommandOutput(err.message);
}
}

async function enableMapTouchEvents() {
setCommandOutput('');

try {
if (maps) {
for (let map of maps) {
map.enableTouch();
}
}
} catch (err: any) {
setCommandOutput(err.message);
}
}

async function destroyMaps() {
setCommandOutput('');
try {
Expand Down Expand Up @@ -170,6 +198,12 @@ const CreateAndDestroyMapPage: React.FC = () => {
>
Remove On Map Click Listeners
</IonButton>
<IonButton expand='block' id="enableTouchEvents" onClick={enableMapTouchEvents}>
Enable Touch Events
</IonButton>
<IonButton expand='block' id="disableTouchEvents" onClick={disableMapTouchEvents}>
Disable Touch Events
</IonButton>
<IonButton expand="block" id="destroyMapButton" onClick={destroyMaps}>
Destroy Maps
</IonButton>
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 @@ -179,9 +179,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 @@ -625,9 +641,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 @@ -171,6 +171,8 @@ export interface FitBoundsArgs {

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 @@ -309,6 +311,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 @@ -136,6 +136,14 @@ export class CapacitorGoogleMapsWeb
}
}

async enableTouch(_args: { id: string }): Promise<void> {
this.maps[_args.id].map.setOptions({ gestureHandling: 'auto' });
}

async disableTouch(_args: { id: string }): Promise<void> {
this.maps[_args.id].map.setOptions({ gestureHandling: 'none' });
}

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