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.
1
let singleTap =UITapGestureRecognizer(target:self, action:#selector(didTap(tap:)))
2
let longTap =UILongPressGestureRecognizer(target:self, action:#selector(didLongPress(tap:)))
Add the gesture recognizers to the NavigationMapView object.
In the didTap() and didLongPress() methods, get the coordinates of the point where the user tapped or long pressed on the map.
1
let coordinates = navigationMapView.convert(tap.location(in: navigationMapView), toCoordinateFrom: navigationMapView)
In the didTap() and didLongPress() methods, add a new destination icon to the map.
1
addNewDestinationIcon(coordinates: coordinates)
In the addNewDestinationIcon() method, remove any existing destination icons from the map.
1
iflet 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.
1
let annotation =NGLPointAnnotation()
2
annotation.coordinate = coordinates
In the addNewDestinationIcon() method, add the annotation object to the