Skip to main content

Long tap animation

Animate camera upon a long tap.
import MapboxMaps
import UIKit

// This examples shows how to animate camera to a long-tapped coordinate.
final class ViewController: UIViewController {
private var mapView: MapView!
private var pointAnnotationManager: PointAnnotationManager!
private var cancelables = Set<AnyCancelable>()

override func viewDidLoad() {

// Center the map over the United States.
let centerCoordinate = CLLocationCoordinate2D(latitude: 39.368279,
longitude: -97.646484)
let options = MapInitOptions(cameraOptions: CameraOptions(center: centerCoordinate, zoom: 2.4))

mapView = MapView(frame: view.bounds, mapInitOptions: options)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

mapView.mapboxMap.onMapLoaded.observeNext { [weak self] _ in
try? self?.mapView.mapboxMap.addImage(UIImage(named: "intermediate-pin")!, id: .blueMarker)

}.store(in: &cancelables)

mapView.gestures.onMapLongPress.observe { [weak self] context in
self?.handleLongPress(at: context.point)
}.store(in: &cancelables)

pointAnnotationManager = mapView.annotations.makePointAnnotationManager()

private func handleLongPress(at location: CGPoint) {
let coordinate = mapView.mapboxMap.coordinate(for: location)

var annotation = PointAnnotation(point: Point(coordinate))
annotation.iconImage = .blueMarker
annotation.iconOffset = [0, 12]

let camera = CameraOptions(center: coordinate) camera, duration: 0.5)

private func haptic() {
let impactFeedbackGenerator = UIImpactFeedbackGenerator(style: .heavy)

private extension String {
static let blueMarker = "blue-marker"
Was this example helpful?