Intercept NavigationMapView click and long click events

Intercept single tap and long click events on NavigationMapView and add corresponding actions to it.

This example shows how to intercept single tap and long click events on NavigationMapView and add corresponding actions to it.

We can get the coordinates from the tap events, so it can also be used to request route info or add markers on the map.

docs-image

For all code examples, refer to Navigation Code Examples

InterceptClickEventViewController view source

1import UIKit
2import NbmapNavigation
3import NbmapCoreNavigation
4import NbmapDirections
5import Nbmap
6
7class InterceptClickEventViewController: UIViewController, NGLMapViewDelegate {
8
9 var navigationMapView: NavigationMapView? {
10 didSet {
11 oldValue?.removeFromSuperview()
12 if let navigationMapView = navigationMapView {
13 view.insertSubview(navigationMapView, at: 0)
14 }
15 }
16 }
17
18 override func viewDidLoad() {
19 super.viewDidLoad()
20
21 self.navigationMapView = NavigationMapView(frame: view.bounds)
22
23 navigationMapView?.userTrackingMode = .followWithHeading
24
25 let singleTap = UITapGestureRecognizer(target: self, action: #selector(didTap(tap:)))
26 navigationMapView?.addGestureRecognizer(singleTap)
27
28 let longTap = UILongPressGestureRecognizer(target: self, action: #selector(didLongPress(tap:)))
29 navigationMapView?.gestureRecognizers?.filter({ $0 is UILongPressGestureRecognizer }).forEach(longTap.require(toFail:))
30 navigationMapView?.addGestureRecognizer(longTap)
31
32
33 navigationMapView?.delegate = self
34
35 self.view.setNeedsLayout()
36
37 }
38
39 @objc func didTap(tap: UITapGestureRecognizer) {
40 guard let navigationMapView = navigationMapView else {
41 return
42 }
43 let coordinates = navigationMapView.convert(tap.location(in: navigationMapView), toCoordinateFrom: navigationMapView)
44 print("Tapped on Map with latitude: " + String(coordinates.latitude) + " longitude: " + String(coordinates.longitude))
45 addNewDestinationIcon(coordinates: coordinates)
46 }
47
48
49 @objc func didLongPress(tap: UILongPressGestureRecognizer) {
50 guard let navigationMapView = navigationMapView, tap.state == .began else {
51 return
52 }
53 let coordinates = navigationMapView.convert(tap.location(in: navigationMapView), toCoordinateFrom: navigationMapView)
54 print("Long Pressed on Map with latitude: " + String(coordinates.latitude) + " longitude: " + String(coordinates.longitude))
55 addNewDestinationIcon(coordinates: coordinates)
56 }
57
58
59 func addNewDestinationIcon(coordinates: CLLocationCoordinate2D){
60 guard let mapView = navigationMapView else {
61 return
62 }
63
64 if let annotation = mapView.annotations?.last {
65 mapView.removeAnnotation(annotation)
66 }
67
68 let annotation = NGLPointAnnotation()
69 annotation.coordinate = coordinates
70 mapView.addAnnotation(annotation)
71 }
72}

The example shows how to intercept single tap and long click events on NavigationMapView. This can be useful for adding custom functionality to the map, such as adding a new destination icon when the user taps or long presses on the map.

The code first creates two UIGestureRecognizer objects: a single tap gesture recognizer and a long press gesture recognizer. These gesture recognizers are then added to the NavigationMapView object. The single tap gesture recognizer will be called when the user taps on the map, and the long press gesture recognizer will be called when the user long presses on the map.

The single tap and long press gesture recognizers both call the didTap() and didLongPress() methods, respectively. These methods first check to make sure that a NavigationMapView object exists. If it does, the methods then get the coordinates of the point where the user tapped or long pressed on the map. The coordinates are then used to add a new destination icon to the map.

The addNewDestinationIcon() method first checks to make sure that a NavigationMapView object exists. If it does, the method then removes any existing destination icons from the map. The method then creates a new NGLPointAnnotation object and sets its coordinate property to the coordinates of the point where the user tapped or long pressed on the map. The annotation object is then added to the NavigationMapView object

  • Create two UIGestureRecognizer objects: a single tap gesture recognizer and a long press gesture recognizer.

    1let singleTap = UITapGestureRecognizer(target: self, action: #selector(didTap(tap:)))
    2let longTap = UILongPressGestureRecognizer(target: self, action: #selector(didLongPress(tap:)))
  • Add the gesture recognizers to the NavigationMapView object.

    1navigationMapView?.addGestureRecognizer(singleTap)
    2navigationMapView?.addGestureRecognizer(longTap)
  • Create the didTap() and didLongPress() methods.

    1 @objc func didTap(tap: UITapGestureRecognizer) {
    2 // ...
    3 }
    4
    5 @objc func didLongPress(tap: UILongPressGestureRecognizer) {
    6 // ...
    7 }
  • In the didTap() and didLongPress() methods, get the coordinates of the point where the user tapped or long pressed on the map.

    1let coordinates = navigationMapView.convert(tap.location(in: navigationMapView), toCoordinateFrom: navigationMapView)
  • In the didTap() and didLongPress() methods, add a new destination icon to the map.

    1addNewDestinationIcon(coordinates: coordinates)
  • In the addNewDestinationIcon() method, remove any existing destination icons from the map.

    1if let annotation = mapView.annotations?.last {
    2 mapView.removeAnnotation(annotation)
    3}
  • In the addNewDestinationIcon() method, create a new NGLPointAnnotation object and set its coordinate property to the coordinates of the point where the user tapped or long pressed on the map.

    1let annotation = NGLPointAnnotation()
    2annotation.coordinate = coordinates
  • In the addNewDestinationIcon() method, add the annotation object to the

    1NavigationMapView object.
    2mapView.addAnnotation(annotation)