Skip to content

Commit

Permalink
add ios drag support
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-ht committed Dec 16, 2021
1 parent 0b05587 commit 1b34a12
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
76 changes: 75 additions & 1 deletion ios/Classes/MapboxMapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import MapboxAnnotationExtension
import UIKit

class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, MapboxMapOptionsSink,
MGLAnnotationControllerDelegate
UIGestureRecognizerDelegate, MGLAnnotationControllerDelegate
{
private var registrar: FlutterPluginRegistrar
private var channel: FlutterMethodChannel?
Expand All @@ -14,11 +14,15 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
private var isFirstStyleLoad = true
private var onStyleLoadedCalled = false
private var mapReadyResult: FlutterResult?
private var previousDragCoordinate: CLLocationCoordinate2D?
private var originDragCoordinate: CLLocationCoordinate2D?
private var dragFeature: MGLFeature?

private var initialTilt: CGFloat?
private var cameraTargetBounds: MGLCoordinateBounds?
private var trackCameraPosition = false
private var myLocationEnabled = false
private var scrollingEnabled = true

private var symbolAnnotationController: MGLSymbolAnnotationController?
private var circleAnnotationController: MGLCircleAnnotationController?
Expand Down Expand Up @@ -80,6 +84,14 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
}
mapView.addGestureRecognizer(longPress)

let pan = UIPanGestureRecognizer(
target: self,
action: #selector(handleMapPan(sender:))
)

pan.delegate = self
mapView.addGestureRecognizer(pan)

if let args = args as? [String: Any] {
Convert.interpretMapboxMapOptions(options: args["options"], delegate: self)
if let initialCameraPosition = args["initialCameraPosition"] as? [String: Any],
Expand Down Expand Up @@ -114,6 +126,13 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
controller.removeStyleAnnotations(annotations.filter { idSet.contains($0.identifier) })
}

func gestureRecognizer(
_: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWith _: UIGestureRecognizer
) -> Bool {
return true
}

func onMethodCall(methodCall: FlutterMethodCall, result: @escaping FlutterResult) {
switch methodCall.method {
case "map#waitForMap":
Expand Down Expand Up @@ -1004,6 +1023,60 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
}
}

/*
* UITapGestureRecognizer
* On pan might invoke the feature#onDrag callback.
*/
@IBAction func handleMapPan(sender: UIPanGestureRecognizer) {
// Get the CGPoint where the user tapped.
print(sender.state)

let point = sender.location(in: mapView)
let coordinate = mapView.convert(point, toCoordinateFrom: mapView)

if sender.state == UIGestureRecognizer.State.began,
sender.numberOfTouches == 1,
let feature = firstFeatureOnLayers(at: point),
let draggable = feature.attribute(forKey: "draggable") as? Bool,
draggable
{
dragFeature = feature
originDragCoordinate = coordinate
previousDragCoordinate = coordinate
mapView.allowsScrolling = false
for gestureRecognizer in mapView.gestureRecognizers! {
if let _ = gestureRecognizer as? UIPanGestureRecognizer {
gestureRecognizer.addTarget(self, action: #selector(handleMapPan))
break
}
}
} else if sender.state == UIGestureRecognizer.State.ended || sender.numberOfTouches != 1 {
sender.state = UIGestureRecognizer.State.ended
mapView.allowsScrolling = scrollingEnabled
dragFeature = nil
originDragCoordinate = nil
previousDragCoordinate = nil
} else if let feature = dragFeature,
let id = feature.identifier,
let previous = previousDragCoordinate,
let origin = originDragCoordinate
{
print("in drag")
channel?.invokeMethod("feature#onDrag", arguments: [
"id": id,
"x": point.x,
"y": point.y,
"originLng": origin.longitude,
"originLat": origin.latitude,
"currentLng": coordinate.longitude,
"currentLat": coordinate.latitude,
"deltaLng": coordinate.longitude - previous.longitude,
"deltaLat": coordinate.latitude - previous.latitude,
])
previousDragCoordinate = coordinate
}
}

/*
* UILongPressGestureRecognizer
* After a long press invoke the map#onMapLongClick callback.
Expand Down Expand Up @@ -1414,6 +1487,7 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma

func setScrollGesturesEnabled(scrollGesturesEnabled: Bool) {
mapView.allowsScrolling = scrollGesturesEnabled
scrollingEnabled = scrollGesturesEnabled
}

func setTiltGesturesEnabled(tiltGesturesEnabled: Bool) {
Expand Down
22 changes: 22 additions & 0 deletions mapbox_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ class MethodChannelMapboxGl extends MapboxGlPlatform {
'latLng': LatLng(lat, lng)
});
break;
case 'feature#onDrag':
final id = call.arguments['id'];
final double x = call.arguments['x'];
final double y = call.arguments['y'];
final double originLat = call.arguments['originLat'];
final double originLng = call.arguments['originLng'];

final double currentLat = call.arguments['currentLat'];
final double currentLng = call.arguments['currentLng'];

final double deltaLat = call.arguments['deltaLat'];
final double deltaLng = call.arguments['deltaLng'];

onFeatureDraggedPlatform({
'id': id,
'point': Point<double>(x, y),
'origin': LatLng(originLat, originLng),
'current': LatLng(currentLat, currentLng),
'delta': LatLng(deltaLat, deltaLng),
});
break;

case 'camera#onMoveStarted':
onCameraMoveStartedPlatform(null);
break;
Expand Down

0 comments on commit 1b34a12

Please sign in to comment.