• Optimization
  • Navigation
  • Tracking
  • Maps
  • Places

Maps Delegate implementation

This example lists all of the NGLMapViewDelegate methods and describes each of them.

For all code examples, refer to Maps Code Examples

MapViewDelegateViewController view source

1import Foundation
2import UIKit
3import Nbmap
4class MapViewDelegateViewController: UIViewController {
5 var nbMapView: NGLMapView! {
6 didSet {
7 oldValue?.removeFromSuperview()
8 if let mapView = nbMapView {
9 configureMapView(nbMapView)
10 view.insertSubview(mapView, at: 0)
11 }
12 }
13 }
14
15 func configureMapView(_ mapView: NGLMapView) {
16 mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
17 //Set the delegate
18 mapView.delegate = self

The code initializes a MapViewDelegateViewController class that subclasses UIViewController. It includes a property nbMapView of type NGLMapView, which represents a map view. The nbMapView property is configured in the configureMapView method, where the autoresizing mask is set and the delegate is assigned. In viewDidLoad, an instance of NGLMapView is created and assigned to nbMapView.

The code extends the MapViewDelegateViewController class to conform to the NGLMapViewDelegate protocol. It implements various delegate methods that provide information and respond to events related to the map view. These methods include:

  • mapView(_:didFinishLoading:): This method is called when the map view finishes loading its style. It creates a new camera object and sets it as the camera for the map view using the fly(to:completionHandler:) method.

  • mapView(_:shouldChangeFrom:to:): This method is called when the map view is about to change from the existing camera to a new camera. It can be used to determine whether the camera change should be allowed or not.

  • mapView(_:shouldChangeFrom:to:reason:): This method is similar to the previous one but provides additional information about the reason for the camera change.

  • mapView(_:regionWillChangeAnimated:) and mapView(_:regionWillChangeWith:animated:): These methods are called when the map view's viewpoint is about to change. They can be used to perform actions or update UI before the change occurs.

  • mapViewRegionIsChanging(_:) and mapView(_:regionIsChangingWith:): These methods are called while the map view's viewpoint is changing, either due to user interaction or animation. They can be used to track the ongoing changes in the viewpoint.

  • mapView(_:regionDidChangeAnimated:) and mapView(_:regionDidChangeWith:animated:): These methods are called when the map view's viewpoint has finished changing. They can be used to perform actions or update UI after the change has occurred.

  • Various other methods are implemented to handle events such as map loading, rendering, user location tracking, managing the appearance of annotations, etc. These methods provide customization options and allow the developer to respond to specific events related to the map view.

Overall, the code sets up a map view, configures its delegate, and implements delegate methods to control its behavior and respond to user interactions and map events.

Have Questions ?