What I did so far was create a map. Then show user location and center it so that map is centered when travelling (car etc)
But now I would like to add a long press gesture so that If a user does the input a pin will be dropped. I have struggled with tutorials and the simulator crashes.
How would I add longPressGesturerecognizer
so that it drops a pin on my mapView
.
Here is my code-
import UIKit
import MapKit
import CoreLocation
class Page2: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{
@IBOutlet var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
//blue dot on the map
self.mapView.showsUserLocation = true
//tracking mode on
self.mapView.userTrackingMode = .follow
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// location Manager Delegate center user on map
private func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: (location?.coordinate.longitude)!)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005)) //zoom on map
self.mapView.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
// print errors
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error){
print("Errors: " + error.localizedDescription)
}
}