Custom Location Source
Customize the location data source for NavigationMapView and Navigation. Obtain location data from user tap actions on the map.
This example shows how to customize your location data source for NavigationMapView and Navigation
-
Custom your location data source for
NavigationMapView, the location data value is from the user tap action on the map -
Custom your location data source for
Navigation

For all code examples, refer to Navigation Code Examples
CustomLocationSourceViewController view source
1import UIKit2import NbmapNavigation3import NbmapCoreNavigation4import Nbmap56class CustomLocationSourceViewController: UIViewController, NGLMapViewDelegate {78var navigationMapView: NavigationMapView? {9didSet {10oldValue?.removeFromSuperview()11if let navigationMapView = navigationMapView {12view.insertSubview(navigationMapView, at: 0)13}14}15}1617var routes : [Route]? {18didSet {19guard routes != nil else{20startButton.isEnabled = false21return22}23startButton.isEnabled = true24}25}2627var currentRouteIndex = 02829var startButton = UIButton()3031override func viewDidLoad() {32super.viewDidLoad()3334navigationMapView = NavigationMapView(frame: view.bounds)3536navigationMapView?.locationManager = CustomMapLocationManager()3738navigationMapView?.userTrackingMode = .followWithHeading3940let singleTap = UILongPressGestureRecognizer(target: self, action: #selector(didLongPress(tap:)))41navigationMapView?.gestureRecognizers?.filter({ $0 is UILongPressGestureRecognizer }).forEach(singleTap.require(toFail:))42navigationMapView?.addGestureRecognizer(singleTap)4344setupStartButton()4546self.view.setNeedsLayout()4748}4950func setupStartButton() {51startButton.setTitle("Start", for: .normal)52startButton.layer.cornerRadius = 553startButton.contentEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)54startButton.backgroundColor = .blue5556startButton.addTarget(self, action: #selector(tappedButton), for: .touchUpInside)57view.addSubview(startButton)58startButton.translatesAutoresizingMaskIntoConstraints = false59startButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -50).isActive = true60startButton.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = true61startButton.titleLabel?.font = UIFont.systemFont(ofSize: 25)62}6364@objc func tappedButton(sender: UIButton) {65guard let routes = self.routes else {66return67}68let navigationService = NBNavigationService(routes: routes, routeIndex: currentRouteIndex,locationSource: CustomNavigationLocationManager())69let navigationOptions = NavigationOptions(navigationService: navigationService)7071let navigationViewController = NavigationViewController(for: routes,navigationOptions: navigationOptions)72navigationViewController.modalPresentationStyle = .fullScreen7374present(navigationViewController, animated: true, completion: nil)75}7677@objc func didLongPress(tap: UILongPressGestureRecognizer) {78guard let navigationMapView = navigationMapView, tap.state == .began else {79return80}81let coordinates = navigationMapView.convert(tap.location(in: navigationMapView), toCoordinateFrom: navigationMapView)82// Note: The destination name can be modified. The value is used in the top banner when arriving at a destination.83let destination = Waypoint(coordinate: coordinates, name: "\(coordinates.latitude),\(coordinates.longitude)")84addNewDestinationcon(coordinates: coordinates)8586guard let currentLocation = navigationMapView.userLocation?.coordinate else { return}87let currentWayPoint = Waypoint.init(coordinate: currentLocation, name: "My Location")8889requestRoutes(origin: currentWayPoint, destination: destination)90}919293func addNewDestinationcon(coordinates: CLLocationCoordinate2D){94guard let mapView = navigationMapView else {95return96}9798if let annotation = mapView.annotations?.last {99mapView.removeAnnotation(annotation)100}101102let annotation = NGLPointAnnotation()103annotation.coordinate = coordinates104mapView.addAnnotation(annotation)105}106107func requestRoutes(origin: Waypoint, destination: Waypoint){108109let options = NavigationRouteOptions(origin: origin, destination: destination)110111Directions.shared.calculate(options) { [weak self] routes, error in112guard let weakSelf = self else {113return114}115guard error == nil else {116print(error!)117return118}119120guard let routes = routes else { return }121122123// Process or display routes information.For example,display the routes,waypoints and duration symbol on the map124weakSelf.navigationMapView?.showRoutes(routes)125weakSelf.navigationMapView?.showRouteDurationSymbol(routes)126127guard let current = routes.first else { return }128weakSelf.navigationMapView?.showWaypoints(current)129weakSelf.routes = routes130}131}132}133134135CustomMapViewLocationManager View source136import Nbmap137import CoreLocation138class CustomMapViewLocationManager: NSObject,NGLLocationManager {139140var delegate: NGLLocationManagerDelegate? {141didSet {142locationManager.delegate = self143}144}145146// Replace with your own location manager147private let locationManager = CLLocationManager()148149var headingOrientation: CLDeviceOrientation {150get {151return locationManager.headingOrientation152}153set {154locationManager.headingOrientation = newValue155}156}157158var desiredAccuracy: CLLocationAccuracy {159get {160return locationManager.desiredAccuracy161}162set {163locationManager.desiredAccuracy = newValue164}165}166167var authorizationStatus: CLAuthorizationStatus {168if #available(iOS 14.0, *) {169return locationManager.authorizationStatus170} else {171return CLLocationManager.authorizationStatus()172}173}174175var activityType: CLActivityType {176get {177return locationManager.activityType178}179set {180locationManager.activityType = newValue181}182}183184@available(iOS 14.0, *)185var accuracyAuthorization: CLAccuracyAuthorization {186return locationManager.accuracyAuthorization187}188189@available(iOS 14.0, *)190func requestTemporaryFullAccuracyAuthorization(withPurposeKey purposeKey: String) {191locationManager.requestTemporaryFullAccuracyAuthorization(withPurposeKey: purposeKey)192}193194func dismissHeadingCalibrationDisplay() {195locationManager.dismissHeadingCalibrationDisplay()196}197198func requestAlwaysAuthorization() {199locationManager.requestAlwaysAuthorization()200}201202func requestWhenInUseAuthorization() {203locationManager.requestWhenInUseAuthorization()204}205206func startUpdatingHeading() {207locationManager.startUpdatingHeading()208}209210func startUpdatingLocation() {211locationManager.startUpdatingLocation()212}213214func stopUpdatingHeading() {215locationManager.stopUpdatingHeading()216}217218func stopUpdatingLocation() {219locationManager.stopUpdatingLocation()220}221222deinit {223locationManager.stopUpdatingLocation()224locationManager.stopUpdatingHeading()225locationManager.delegate = nil226delegate = nil227}228229}230// MARK: - CLLocationManagerDelegate , Please replace with your location data source delegate231extension CustomMapLocationManager : CLLocationManagerDelegate {232233func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {234delegate?.locationManager(self, didUpdate: locations)235}236237func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {238delegate?.locationManager(self, didUpdate: newHeading)239}240241func locationManagerShouldDisplayHeadingCalibration(_ manager: CLLocationManager) -> Bool {242return delegate?.locationManagerShouldDisplayHeadingCalibration(self) ?? false243}244245func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {246delegate?.locationManager(self, didFailWithError: error)247}248249@available(iOS 14.0, *)250func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {251delegate?.locationManagerDidChangeAuthorization(self)252}253}254255256CustomNavigationLocationManager view source257import NbmapCoreNavigation258import CoreLocation259class CustomNavigationLocationManager : NavigationLocationManager {260261override var delegate: CLLocationManagerDelegate? {262didSet {263locationManager.delegate = self264}265}266267// Replace with your own location manager268private let locationManager = CLLocationManager()269270override var headingOrientation: CLDeviceOrientation {271get {272return locationManager.headingOrientation273}274set {275locationManager.headingOrientation = newValue276}277}278279override var desiredAccuracy: CLLocationAccuracy {280get {281return locationManager.desiredAccuracy282}283set {284locationManager.desiredAccuracy = newValue285}286}287288override var authorizationStatus: CLAuthorizationStatus {289if #available(iOS 14.0, *) {290return locationManager.authorizationStatus291} else {292return CLLocationManager.authorizationStatus()293}294}295296override var activityType: CLActivityType {297get {298return locationManager.activityType299}300set {301locationManager.activityType = newValue302}303}304305@available(iOS 14.0, *)306override var accuracyAuthorization: CLAccuracyAuthorization {307return locationManager.accuracyAuthorization308}309310@available(iOS 14.0, *)311override func requestTemporaryFullAccuracyAuthorization(withPurposeKey purposeKey: String) {312locationManager.requestTemporaryFullAccuracyAuthorization(withPurposeKey: purposeKey)313}314315override func dismissHeadingCalibrationDisplay() {316locationManager.dismissHeadingCalibrationDisplay()317}318319override func requestAlwaysAuthorization() {320locationManager.requestAlwaysAuthorization()321}322323override func requestWhenInUseAuthorization() {324locationManager.requestWhenInUseAuthorization()325}326327override func startUpdatingHeading() {328locationManager.startUpdatingHeading()329}330331override func startUpdatingLocation() {332locationManager.startUpdatingLocation()333}334335override func stopUpdatingHeading() {336locationManager.stopUpdatingHeading()337}338339override func stopUpdatingLocation() {340locationManager.stopUpdatingLocation()341}342343deinit {344locationManager.stopUpdatingLocation()345locationManager.stopUpdatingHeading()346locationManager.delegate = nil347delegate = nil348}349350}351// MARK: - CLLocationManagerDelegate352extension CustomNavigationLocationManager : CLLocationManagerDelegate {353354func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {355delegate?.locationManager!(self, didUpdateLocations: locations)356}357358func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {359delegate?.locationManager!(self, didUpdateHeading: newHeading)360}361362func locationManagerShouldDisplayHeadingCalibration(_ manager: CLLocationManager) -> Bool {363return delegate?.locationManagerShouldDisplayHeadingCalibration!(self) ?? false364}365366func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {367delegate?.locationManager!(self, didFailWithError: error)368}369370@available(iOS 14.0, *)371func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {372delegate?.locationManagerDidChangeAuthorization!(self)373}374}375