3
import NbmapCoreNavigation
7
class CustomCameraController: UIViewController, NGLMapViewDelegate, NavigationViewControllerDelegate {
9
var mapView: NavigationMapView? {
11
oldValue?.removeFromSuperview()
12
if let mapView = mapView {
13
view.insertSubview(mapView, at: 0)
18
var routes : [Route]? {
20
guard let routes = routes,
21
let current = routes.first
23
mapView?.removeRoutes()
24
mapView?.removeRouteDurationSymbol()
25
if let annotation = mapView?.annotations?.last {
26
mapView?.removeAnnotation(annotation)
32
mapView?.showRoutes(routes)
33
mapView?.showWaypoints(current)
34
mapView?.showRouteDurationSymbol(routes)
36
fitCameraWithRoutes(routes: routes)
40
var startButton = UIButton()
42
var trackingImage = UIImageView()
44
override func viewDidLoad() {
47
self.mapView = NavigationMapView(frame: view.bounds)
49
mapView?.userTrackingMode = .followWithHeading
51
let singleTap = UILongPressGestureRecognizer(target: self, action: #selector(didLongPress(tap:)))
52
mapView?.gestureRecognizers?.filter({ $0 is UILongPressGestureRecognizer }).forEach(singleTap.require(toFail:))
53
mapView?.addGestureRecognizer(singleTap)
54
mapView?.delegate = self
57
setupLocationTrackingButton()
60
func setupStartButton() {
61
startButton.setTitle("Start", for: .normal)
62
startButton.layer.cornerRadius = 5
63
startButton.contentEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
64
startButton.backgroundColor = .blue
66
startButton.addTarget(self, action: #selector(tappedButton), for: .touchUpInside)
67
view.addSubview(startButton)
68
startButton.translatesAutoresizingMaskIntoConstraints = false
69
startButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -50).isActive = true
70
startButton.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = true
71
startButton.titleLabel?.font = UIFont.systemFont(ofSize: 25)
74
func setupLocationTrackingButton(){
75
trackingImage.isUserInteractionEnabled = true
76
trackingImage.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(trackingClick)))
77
trackingImage.image = UIImage(named: "my_location")
78
view.addSubview(trackingImage)
79
startButton.translatesAutoresizingMaskIntoConstraints = false
80
startButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -50).isActive = true
81
startButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor,constant: 16).isActive = true
84
@objc func didLongPress(tap: UILongPressGestureRecognizer) {
85
guard let mapView = mapView, tap.state == .began else {
88
let coordinates = mapView.convert(tap.location(in: mapView), toCoordinateFrom: mapView)
89
let destination = Waypoint(coordinate: coordinates, name: "\(coordinates.latitude),\(coordinates.longitude)")
91
guard let currentLocation = mapView.userLocation?.coordinate else { return}
92
let currentWayPoint = Waypoint.init(coordinate: currentLocation, name: "My Location")
94
requestRoutes(origin: currentWayPoint, destination: destination)
98
@objc func tappedButton(sender: UIButton) {
99
guard let routes = self.routes else {
102
var engineConfig = NavigationEngineConfig()
104
engineConfig.minNavigationCameraZoom = 16
105
engineConfig.maxNavigationCameraZoom = 18
106
let navigationService = NBNavigationService(routes: routes, routeIndex: 0,navigationEngConfig: engineConfig)
108
let navigationOptions = NavigationOptions(navigationService: navigationService)
109
let navigationViewController = NavigationViewController(for: routes,navigationOptions: navigationOptions)
110
navigationViewController.modalPresentationStyle = .fullScreen
112
navigationViewController.delegate = self
115
present(navigationViewController, animated: true, completion: nil)
118
@objc func trackingClick() {
119
guard let location = mapView?.userLocation else {
122
guard let newCamera = mapView?.camera else {
125
newCamera.centerCoordinate = location.coordinate
126
newCamera.viewingDistance = 1000
127
mapView?.setCamera(newCamera, animated: true)
130
func fitCameraWithRoutes(routes: [Route]) {
131
guard let mapView = mapView else {
135
var coordinates: [CLLocationCoordinate2D] = []
136
routes.forEach({route in
137
coordinates.append(contentsOf: route.coordinates!)
139
let polyLine = NGLPolyline(coordinates: coordinates, count: UInt(coordinates.count))
140
let camera = mapView.cameraThatFitsShape(polyLine, direction: mapView.camera.heading, edgePadding: UIEdgeInsets(top: view.safeAreaInsets.top, left: view.safeAreaInsets.left, bottom: view.safeAreaInsets.bottom, right: view.safeAreaInsets.right))
141
mapView.setCamera(camera, animated: true)
144
func requestRoutes(origin: Waypoint, destination: Waypoint){
145
let options = NavigationRouteOptions(origin: origin, destination: destination)
147
Directions.shared.calculate(options) { [weak self] routes, error in
148
guard let weakSelf = self else {
151
guard error == nil else {
156
guard let routes = routes else { return }
157
weakSelf.routes = routes