forked from mrustaa/ContainerController
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMapViewManager.swift
More file actions
266 lines (183 loc) · 8.34 KB
/
MapViewManager.swift
File metadata and controls
266 lines (183 loc) · 8.34 KB
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
//
// MapViewController.swift
// PatternsSwift
//
// Created by mrustaa on 19/05/2020.
// Copyright © 2020 mrustaa. All rights reserved.
//
import UIKit
import MapKit
import CoreLocation
import ContainerControllerSwift
class MapViewManager: NSObject {
// MARK: Map Properties
var mapView: MKMapView?
var compass: MKCompassButton!
var locationManager: CLLocationManager?
var selectAnnotation: MKPointAnnotation?
var routeOverlay: MKOverlay?
var setMoveMyLocationOnce: Bool = false
// MARK: - Callbacks
var changeRegionCallback: (() -> ())?
var selectPinCallback: (() -> ())?
// MARK: - Init
public init(mapView: MKMapView) {
super.init()
mapView.delegate = self
self.mapView = mapView
loadLocation()
updateCompass()
}
// MARK: - Location Manager
func loadLocation() {
DispatchQueue.global().async {
if CLLocationManager.locationServicesEnabled() {
DispatchQueue.main.async {
self.locationManager = CLLocationManager()
self.locationManager?.delegate = self
self.locationManager?.desiredAccuracy = kCLLocationAccuracyBest
#if os(iOS)
self.locationManager?.requestAlwaysAuthorization()
#endif
self.locationManager?.startUpdatingLocation()
}
}
}
}
// MARK: - Map Compass
func updateCompass() {
mapView?.showsCompass = false
compass = MKCompassButton(mapView:mapView)
compass.x = (ContainerDevice.width - 47)
compass.compassVisibility = .adaptive
mapView?.addSubview(compass)
}
public func closeRoute(showSelectPin: Bool) {
if let route = routeOverlay {
mapView?.removeOverlay(route)
}
selectPinAnimation(show: showSelectPin)
}
public func selectPinAnimation(show: Bool) {
if let selectPin = selectAnnotation {
if show {
mapView?.selectAnnotation(selectPin, animated: true)
} else {
mapView?.deselectAnnotation(selectPin, animated: true)
}
}
}
public func addMapPinFrom(longPress: UILongPressGestureRecognizer) {
guard let mapView = mapView else { return }
if let anotationMap = selectAnnotation {
mapView.removeAnnotations([anotationMap]);
}
let point: CGPoint = longPress.location(in: mapView)
let coordinate: CLLocationCoordinate2D = mapView.convert(point, toCoordinateFrom: mapView)
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
mapView.addAnnotation(annotation)
selectAnnotation = annotation
mapView.selectAnnotation(annotation, animated: true)
}
// MARK: - Map Show Route
public func showRouteOnMapMyLocation() {
if let pickupCoord = mapView?.userLocation.location?.coordinate,
let destinationCoord = selectAnnotation {
showRouteOnMap(pickupCoordinate: pickupCoord, destinationCoordinate: destinationCoord.coordinate)
}
}
public func showRouteOnMap(pickupCoordinate: CLLocationCoordinate2D, destinationCoordinate: CLLocationCoordinate2D) {
closeRoute(showSelectPin: false)
let sourcePlacemark = MKPlacemark(coordinate: pickupCoordinate, addressDictionary: nil)
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
let destinationPlacemark = MKPlacemark(coordinate: destinationCoordinate, addressDictionary: nil)
let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
let directionRequest = MKDirections.Request()
directionRequest.source = sourceMapItem
directionRequest.destination = destinationMapItem
directionRequest.transportType = .automobile
let directions = MKDirections(request: directionRequest)
directions.calculate { [weak self] (response, error) -> Void in
guard let _self = self else { return }
guard let response = response else { return }
let route = response.routes[0]
_self.routeOverlay = route.polyline
_self.mapView?.addOverlay((route.polyline), level: MKOverlayLevel.aboveRoads)
let rect: MKMapRect = route.polyline.boundingMapRect
let coordReg: MKCoordinateRegion = MKCoordinateRegion(rect)
let mapButtonWidth: CGFloat = 45
let padding: CGFloat = 35
let bottom: CGFloat = ContainerDevice.isPortrait ? (261 + padding) : padding
let left: CGFloat = ContainerDevice.isPortrait ? padding : (261 + padding * 2)
let right: CGFloat = (mapButtonWidth + 8 + padding)
let insets = UIEdgeInsets(top: padding, left: left, bottom: bottom, right: right)
_self.mapView?.setVisibleRegion(mapRegion: coordReg, edgePadding: insets, animated: true)
}
}
}
// MARK: - Map-Delegate
extension MapViewManager: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
if !animated {
changeRegionCallback?()
}
}
func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
if !animated {
changeRegionCallback?()
}
}
// MARK: - Map Select-Pin
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let region = MKCoordinateRegion(center: view.annotation!.coordinate, span: mapView.region.span)
mapView.setRegion(region, animated: true)
selectPinCallback?()
if let route = routeOverlay {
mapView.removeOverlay(route)
}
}
// MARK: - Map Route-Color
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = Colors.rgb(17, 147, 255)
renderer.lineWidth = 8.0
return renderer
}
}
// MARK: - Update Location Delegate
extension MapViewManager: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if setMoveMyLocationOnce { return }
if let location = locations.last {
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let radius: CLLocationDistance = 3200
let region = MKCoordinateRegion(center: center, latitudinalMeters: radius, longitudinalMeters: radius)
mapView?.setRegion(region, animated: false)
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: location.coordinate.latitude + 0.01, longitude: location.coordinate.longitude + 0.01)
mapView?.addAnnotation(annotation)
selectAnnotation = annotation
setMoveMyLocationOnce = true
}
}
}
// MARK: - Map Extension
extension MKCoordinateRegion {
var mapRect: MKMapRect {
get {
let a = MKMapPoint( CLLocationCoordinate2DMake(
self.center.latitude + self.span.latitudeDelta / 2,
self.center.longitude - self.span.longitudeDelta / 2))
let b = MKMapPoint( CLLocationCoordinate2DMake(
self.center.latitude - self.span.latitudeDelta / 2,
self.center.longitude + self.span.longitudeDelta / 2))
return MKMapRect(x: min(a.x,b.x), y: min(a.y,b.y), width: abs(a.x-b.x), height: abs(a.y-b.y))
}
}
}
extension MKMapView {
func setVisibleRegion(mapRegion: MKCoordinateRegion, edgePadding insets: UIEdgeInsets, animated animate: Bool) {
self.setVisibleMapRect(mapRegion.mapRect, edgePadding: insets , animated: animate)
}
}