-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathFlutterMapView.swift
358 lines (307 loc) · 14.2 KB
/
FlutterMapView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//
// FlutterAppleMap.swift
// apple_maps_flutter
//
// Created by Luis Thein on 09.10.19.
//
import Foundation
import MapKit
import CoreLocation
enum BUTTON_IDS: Int {
case LOCATION = 100
}
class FlutterMapView: MKMapView, UIGestureRecognizerDelegate {
weak var mapContainerView: UIView?
weak var channel: FlutterMethodChannel?
var oldBounds: CGRect?
var options: Dictionary<String, Any>?
var isMyLocationButtonShowing: Bool? = false
fileprivate let locationManager: CLLocationManager = CLLocationManager()
let mapTypes: Array<MKMapType> = [
MKMapType.standard,
MKMapType.satellite,
MKMapType.hybrid,
]
let userTrackingModes: Array<MKUserTrackingMode> = [
MKUserTrackingMode.none,
MKUserTrackingMode.follow,
MKUserTrackingMode.followWithHeading,
]
convenience init(channel: FlutterMethodChannel, options: Dictionary<String, Any>) {
self.init(frame: CGRect.zero)
self.channel = channel
self.options = options
initialiseTapGestureRecognizers()
}
var actualHeading: CLLocationDirection {
get {
if mapContainerView != nil {
var heading: CLLocationDirection = fabs(180 * asin(Double(mapContainerView!.transform.b)) / .pi)
if mapContainerView!.transform.b <= 0 {
if mapContainerView!.transform.a >= 0 {
// do nothing
} else {
heading = 180 - heading
}
} else {
if mapContainerView!.transform.a <= 0 {
heading = heading + 180
} else {
heading = 360 - heading
}
}
return heading
}
return CLLocationDirection.zero
}
}
// To calculate the displayed region we have to get the layout bounds.
// Because the self is layed out using an auto layout we have to call
// setCenterCoordinate after the self was layed out.
override func layoutSubviews() {
// Only update the map in layoutSubviews if the bounds changed
if self.bounds != oldBounds {
if self.options != nil {
self.interpretOptions(options: self.options!)
}
if #available(iOS 9.0, *) {
setCenterCoordinateWithAltitude(centerCoordinate: centerCoordinate, zoomLevel: zoomLevel, animated: false)
mapContainerView = self.findViewOfType("MKScrollContainerView", inView: self)
} else {
setCenterCoordinateRegion(centerCoordinate: centerCoordinate, zoomLevel: zoomLevel, animated: false)
}
}
oldBounds = self.bounds
}
override func didMoveToSuperview() {
if oldBounds != CGRect.zero {
oldBounds = CGRect.zero
}
}
private func findViewOfType(_ viewType: String, inView view: UIView) -> UIView? {
// function scans subviews recursively and returns
// reference to the found one of a type
if view.subviews.count > 0 {
for v in view.subviews {
let valueDescription = v.description
let keywords = viewType
if valueDescription.range(of: keywords) != nil {
return v
}
if let inSubviews = self.findViewOfType(viewType, inView: v) {
return inSubviews
}
}
return nil
} else {
return nil
}
}
func interpretOptions(options: Dictionary<String, Any>) {
if let isCompassEnabled: Bool = options["compassEnabled"] as? Bool {
if #available(iOS 9.0, *) {
self.showsCompass = isCompassEnabled
self.mapTrackingButton(isVisible: self.isMyLocationButtonShowing ?? false)
}
}
if let padding: Array<Any> = options["padding"] as? Array<Any> {
var margins = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
if padding.count >= 1, let top: Double = padding[0] as? Double {
margins.top = CGFloat(top)
}
if padding.count >= 2, let left: Double = padding[1] as? Double {
margins.left = CGFloat(left)
}
if padding.count >= 3, let bottom: Double = padding[2] as? Double {
margins.bottom = CGFloat(bottom)
}
if padding.count >= 4, let right: Double = padding[3] as? Double {
margins.right = CGFloat(right)
}
self.layoutMargins = margins
}
if let mapType: Int = options["mapType"] as? Int {
self.mapType = self.mapTypes[mapType]
}
if let colorScheme: Int = options["colorScheme"] as? Int {
if #available(iOS 13.0, *) {
self.overrideUserInterfaceStyle = UIUserInterfaceStyle(rawValue: colorScheme) ?? .unspecified
}
}
if let trafficEnabled: Bool = options["trafficEnabled"] as? Bool {
if #available(iOS 9.0, *) {
self.showsTraffic = trafficEnabled
} else {
// do nothing
}
}
if let rotateGesturesEnabled: Bool = options["rotateGesturesEnabled"] as? Bool {
self.isRotateEnabled = rotateGesturesEnabled
}
if let scrollGesturesEnabled: Bool = options["scrollGesturesEnabled"] as? Bool {
self.isScrollEnabled = scrollGesturesEnabled
}
if let pitchGesturesEnabled: Bool = options["pitchGesturesEnabled"] as? Bool {
self.isPitchEnabled = pitchGesturesEnabled
}
if let zoomGesturesEnabled: Bool = options["zoomGesturesEnabled"] as? Bool{
self.isZoomEnabled = zoomGesturesEnabled
}
if let myLocationEnabled: Bool = options["myLocationEnabled"] as? Bool {
if (myLocationEnabled) {
self.setUserLocation()
} else {
self.removeUserLocation()
}
}
if let myLocationButtonEnabled: Bool = options["myLocationButtonEnabled"] as? Bool {
self.mapTrackingButton(isVisible: myLocationButtonEnabled)
}
if let userTackingMode: Int = options["trackingMode"] as? Int {
self.setUserTrackingMode(self.userTrackingModes[userTackingMode], animated: false)
}
if let minMaxZoom: Array<Any> = options["minMaxZoomPreference"] as? Array<Any>{
if let _minZoom: Double = minMaxZoom[0] as? Double {
self.minZoomLevel = _minZoom
}
if let _maxZoom: Double = minMaxZoom[1] as? Double {
self.maxZoomLevel = _maxZoom
}
}
if let insetsSafeArea: Bool = options["insetsLayoutMarginsFromSafeArea"] as? Bool {
if #available(iOS 11.0, *) {
self.insetsLayoutMarginsFromSafeArea = insetsSafeArea
}
}
}
func setUserLocation() {
let authorizationStatus = CLLocationManager.authorizationStatus()
switch authorizationStatus {
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
break
case .authorizedAlways:
fallthrough
case .authorizedWhenInUse:
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.startUpdatingLocation()
self.showsUserLocation = true
break
default:
print("\(authorizationStatus.rawValue) is not supported.")
}
}
func removeUserLocation() {
locationManager.stopUpdatingLocation()
self.showsUserLocation = false
}
// Functions used for the mapTrackingButton
func mapTrackingButton(isVisible visible: Bool) {
self.isMyLocationButtonShowing = visible
if let _locationButton = self.viewWithTag(BUTTON_IDS.LOCATION.rawValue) {
_locationButton.removeFromSuperview()
}
if visible {
let buttonContainer = UIView()
if #available(iOS 9.0, *) {
buttonContainer.translatesAutoresizingMaskIntoConstraints = false
buttonContainer.widthAnchor.constraint(equalToConstant: 35).isActive = true
buttonContainer.heightAnchor.constraint(equalToConstant: 35).isActive = true
buttonContainer.layer.cornerRadius = 8
buttonContainer.tag = BUTTON_IDS.LOCATION.rawValue
buttonContainer.backgroundColor = .white
if #available(iOS 11.0, *) {
let userTrackingButton = MKUserTrackingButton(mapView: self)
userTrackingButton.translatesAutoresizingMaskIntoConstraints = false
buttonContainer.addSubview(userTrackingButton)
userTrackingButton.centerXAnchor.constraint(equalTo: buttonContainer.centerXAnchor).isActive = true
userTrackingButton.centerYAnchor.constraint(equalTo: buttonContainer.centerYAnchor).isActive = true
} else {
let locationButton = UIButton(type: UIButton.ButtonType.custom) as UIButton
let image = UIImage(named: "outline_near_me")
locationButton.translatesAutoresizingMaskIntoConstraints = false
locationButton.setImage(image, for: .normal)
locationButton.imageView?.tintColor = .blue
locationButton.addTarget(self, action: #selector(centerMapOnUserButtonClicked), for:.touchUpInside)
buttonContainer.addSubview(locationButton)
locationButton.centerXAnchor.constraint(equalTo: buttonContainer.centerXAnchor).isActive = true
locationButton.centerYAnchor.constraint(equalTo: buttonContainer.centerYAnchor).isActive = true
}
self.addSubview(buttonContainer)
buttonContainer.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -5 - self.layoutMargins.right).isActive = true
buttonContainer.topAnchor.constraint(equalTo: self.topAnchor, constant: self.showsCompass ? 50 : 5 + self.layoutMargins.top).isActive = true
}
}
}
@objc func centerMapOnUserButtonClicked() {
self.setUserTrackingMode(MKUserTrackingMode.follow, animated: true)
}
func getMapViewAnnotations() -> [FlutterAnnotation?] {
let flutterAnnotations = self.annotations as? [FlutterAnnotation] ?? []
let sortedAnnotations = flutterAnnotations.sorted(by: { $0.zIndex < $1.zIndex })
return sortedAnnotations
}
// Functions used for GestureRecognition
private func initialiseTapGestureRecognizers() {
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(onMapGesture))
panGesture.maximumNumberOfTouches = 2
panGesture.delegate = self
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(onMapGesture))
pinchGesture.delegate = self
let rotateGesture = UIRotationGestureRecognizer(target: self, action: #selector(onMapGesture))
rotateGesture.delegate = self
let tiltGesture = UISwipeGestureRecognizer(target: self, action: #selector(onMapGesture))
tiltGesture.numberOfTouchesRequired = 2
tiltGesture.direction = .up
tiltGesture.direction = .down
let doubleTapGesture = UITapGestureRecognizer(target: self, action: nil)
doubleTapGesture.numberOfTapsRequired = 2
let longTapGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap))
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onTap))
tapGesture.require(toFail: doubleTapGesture) // only recognize taps that are not involved in zooming
self.addGestureRecognizer(panGesture)
self.addGestureRecognizer(pinchGesture)
self.addGestureRecognizer(rotateGesture)
self.addGestureRecognizer(tiltGesture)
self.addGestureRecognizer(longTapGesture)
self.addGestureRecognizer(doubleTapGesture)
self.addGestureRecognizer(tapGesture)
}
@objc func onMapGesture(sender: UIGestureRecognizer) {
let locationOnMap = self.region.center // self.convert(locationInView, toCoordinateFrom: self)
let zoom = self.calculatedZoomLevel
let pitch = self.camera.pitch
let heading = self.actualHeading
self.updateCameraValues()
channel?.invokeMethod("camera#onMove", arguments: ["position": ["heading": heading, "target": [locationOnMap.latitude, locationOnMap.longitude], "pitch": pitch, "zoom": zoom]])
}
@objc func longTap(sender: UIGestureRecognizer){
if sender.state == .began {
let locationInView = sender.location(in: self)
let locationOnMap = self.convert(locationInView, toCoordinateFrom: self)
channel?.invokeMethod("map#onLongPress", arguments: ["position": [locationOnMap.latitude, locationOnMap.longitude]])
}
}
@objc func onTap(tap: UITapGestureRecognizer) {
if tap.state == .recognized {
TouchHandler.handleMapTaps(tap: tap, overlays: self.overlays, channel: self.channel, in: self)
}
}
func updateCameraValues() {
if oldBounds != nil && oldBounds != CGRect.zero {
self.updateStoredCameraValues(newZoomLevel: calculatedZoomLevel, newPitch: camera.pitch, newHeading: actualHeading)
}
}
// Always allow multiple gestureRecognizers
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
func distanceOfCGPoints(_ a: CGPoint, _ b: CGPoint) -> CGFloat {
let xDist = a.x - b.x
let yDist = a.y - b.y
return CGFloat(sqrt(xDist * xDist + yDist * yDist))
}
}