• Optimization
  • Navigation
  • Tracking
  • Maps
  • Places

Custom Location Source

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

  • Custom your location data source for NGLMapView
docs-image

For all code examples, refer to Maps Code Examples

CustomLocationSourceViewController view source

1import Foundation
2import UIKit
3import Nbmap
4class 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 /**

CustomMapLocationManager

1import Nbmap
2import CoreLocation
3class 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 {

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.

Have Questions ?