Events
Contents
The Maps SDK provides several ways to listen and act upon events fired by the map. The generic listeners are mentioned below. However, you may also find specific callbacks exist for specific APIs or uses.
Map click events
The Maps SDK provides a set of built-in gesture recognizers. You can customize or supplement these gestures according to your use case. You can see which gesture recognizers are on your TMGLMapView
by accessing the gestureRecognizers
property on your map.
func addSingleTapGestureRecognizer() {
// add a single tag gesture recognizer
let singleTap = UITapGestureRecognizer(target: self, action: #selector(handleMapTap(sender:)))
for recognizer in mapView.gestureRecognizers! where recognizer is UITapGestureRecognizer {
singleTap.require(toFail: recognizer)
}
mapView.addGestureRecognizer(singleTap)
}
@objc @IBAction func handleMapTap(sender: UITapGestureRecognizer) {
// Convert tap location (CGPoint) to geographic coordinate (CLLocationCoordinate2D).
let tapPoint: CGPoint = sender.location(in: mapView)
let tapCoordinate: CLLocationCoordinate2D = mapView.convert(tapPoint, toCoordinateFrom: mapView)
print("You tapped at: \(tapCoordinate.latitude), \(tapCoordinate.longitude)")
}
Map change events
While building or changing the map, the MapView object goes through many phases. The TMGLMapViewDelegate protocol defines a set of optional methods that you can use to receive map-related update messages, allowing you to capture when a specific map event has triggered. For example:
func mapViewDidFinishLoadingMap(_ mapView: TMGLMapView) {
// The map finished loading
}
There are a variety of other events available that are used in a similar manner to the above:
Method | Description |
---|---|
mapViewWillStartLoadingMap | Map about to start loading a new style. |
mapViewWillStartRenderingFrame | Map is about to start rendering a frame. |
mapViewDidFinishRenderingFrame | Map finished rendering the frame. |
didFinishLoadingStyle | Style finished loading. |
Camera change events
The map’s camera is used to define how the map is viewed; zoom level, rotation, etc. Go here for more details on the Camera itself. There are a few callbacks that you can listen to that relate to some of the camera’s movements, usually related around the beginning and end of a camera movement.
Method | Description |
---|---|
regionWillChangeAnimated | The currently displayed map camera is about to change. |
mapViewRegionIsChanging | The currently displayed map camera is changing. |
regionDidChangeAnimated | The currently displayed map camera finished changing. |
Movement events
Separately from the camera movement events, there is also a callback that is fired after the user moves the map.
Method | Description |
---|---|
didChangeUserTrackingMode | This method is called after the map view asynchronously changes to reflect the new user tracking mode, for example by beginning to zoom or rotate. |