Custom Location Source

This example shows how to customize your location data source for NGLMapView.

  • Custom your location data source for NGLMapView
documentation image

For all code examples, refer to Maps Code Examples

CustomLocationSourceViewController view source

1
import Foundation
2
import UIKit
3
import Nbmap
4
class CustomLocationSourceViewController: UIViewController {
5
var nbMapView: NGLMapView! {
6
didSet {
7
oldValue?.removeFromSuperview()
8
if let mapView = nbMapView {
9
view.insertSubview(mapView, at: 0)
10
}
11
}
12
}
13
override func viewDidLoad() {
14
super.viewDidLoad()
15
nbMapView = NGLMapView(frame:self.view.bounds)
16
nbMapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
17
18
/**
19
Custom the location source , The locationManager that this map view uses to start and stop the delivery of
20
location-related updates.
21
To receive the current user location, implement the
22
`-[NGLMapViewDelegate mapView:didUpdateUserLocation:]` and
23
`-[NGLMapViewDelegate mapView:didFailToLocateUserWithError:]` methods.
24
If setting this property to `nil` or if no custom manager is provided this
25
property is set to the default location manager.
26
`NGLMapView` uses a default location manager. If you want to substitute your
27
own location manager, you should do so by setting this property before setting
28
`showsUserLocation` to `YES`. To restore the default location manager,
29
set this property to `nil`.
30
*/
31
nbMapView.locationManager = CustomMapLocationManager()
32
nbMapView.showsUserLocation = true
33
nbMapView.userTrackingMode = .follow
34
}
35
}

CustomMapLocationManager

1
import Nbmap
2
import CoreLocation
3
class CustomMapLocationManager: NSObject,NGLLocationManager {
4
5
var delegate: NGLLocationManagerDelegate? {
6
didSet {
7
locationManager.delegate = self
8
}
9
}
10
11
// Replay with your own location manager
12
private let locationManager = CLLocationManager()
13
14
var headingOrientation: CLDeviceOrientation {
15
get {
16
return locationManager.headingOrientation
17
}
18
set {
19
locationManager.headingOrientation = newValue
20
}
21
}
22
23
var desiredAccuracy: CLLocationAccuracy {
24
get {
25
return locationManager.desiredAccuracy
26
}
27
set {
28
locationManager.desiredAccuracy = newValue
29
}
30
}
31
32
var authorizationStatus: CLAuthorizationStatus {
33
if #available(iOS 14.0, *) {
34
return locationManager.authorizationStatus
35
} else {
36
return CLLocationManager.authorizationStatus()
37
}
38
}
39
40
var activityType: CLActivityType {
41
get {
42
return locationManager.activityType
43
}
44
set {
45
locationManager.activityType = newValue
46
}
47
}
48
49
@available(iOS 14.0, *)
50
var accuracyAuthorization: CLAccuracyAuthorization {
51
return locationManager.accuracyAuthorization
52
}
53
54
@available(iOS 14.0, *)
55
func requestTemporaryFullAccuracyAuthorization(withPurposeKey purposeKey: String) {
56
locationManager.requestTemporaryFullAccuracyAuthorization(withPurposeKey: purposeKey)
57
}
58
59
func dismissHeadingCalibrationDisplay() {
60
locationManager.dismissHeadingCalibrationDisplay()
61
}
62
63
func requestAlwaysAuthorization() {
64
locationManager.requestAlwaysAuthorization()
65
}
66
67
func requestWhenInUseAuthorization() {
68
locationManager.requestWhenInUseAuthorization()
69
}
70
71
func startUpdatingHeading() {
72
locationManager.startUpdatingHeading()
73
}
74
75
func startUpdatingLocation() {
76
locationManager.startUpdatingLocation()
77
}
78
79
func stopUpdatingHeading() {
80
locationManager.stopUpdatingHeading()
81
}
82
83
func stopUpdatingLocation() {
84
locationManager.stopUpdatingLocation()
85
}
86
87
deinit {
88
locationManager.stopUpdatingLocation()
89
locationManager.stopUpdatingHeading()
90
locationManager.delegate = nil
91
delegate = nil
92
}
93
94
}
95
// MARK: - CLLocationManagerDelegate
96
extension CustomMapLocationManager : CLLocationManagerDelegate {
97
98
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
99
delegate?.locationManager(self, didUpdate: locations)
100
}
101
102
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
103
delegate?.locationManager(self, didUpdate: newHeading)
104
}
105
106
func locationManagerShouldDisplayHeadingCalibration(_ manager: CLLocationManager) -> Bool {
107
return delegate?.locationManagerShouldDisplayHeadingCalibration(self) ?? false
108
}
109
110
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
111
delegate?.locationManager(self, didFailWithError: error)
112
}
113
114
@available(iOS 14.0, *)
115
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
116
delegate?.locationManagerDidChangeAuthorization(self)
117
}
118
}

The example code implements a custom location source (CustomLocationSourceViewController) along with a custom location manager (CustomMapLocationManager).

CustomLocationSourceViewController is a subclass of UIViewController responsible for displaying a map view and setting up a custom location source

  • Initializing a map view (NGLMapView) and setting its frame to match the bounds of the current view.

  • Adding the map view as a subview to the current view.

  • Customizing the location source by assigning an instance of CustomMapLocationManager to the locationManager property of the map view.

  • Setting showsUserLocation to true to display the user's location on the map.

  • Setting userTrackingMode to .follow to track the user's location.

CustomMapLocationManager is a custom location manager that conforms to the NGLLocationManager protocol. It utilizes a CLLocationManager as its private property and sets itself as the delegate.

  • It implements the NGLLocationManagerDelegate protocol by setting the locationManager delegate to itself.

  • Various properties and methods of the location manager are mapped to the private locationManager instance using the delegate pattern.

  • It implements methods from the CLLocationManagerDelegate protocol and forwards these methods to the delegate object.

Summary: The provided code establishes a custom location source by utilizing a custom location manager. By setting the custom location manager, it is possible to replace the default location manager and achieve more flexibility and customization in handling location-related updates and delegate callbacks.

© 2024 NextBillion.ai all rights reserved.