If I add a new custom annotation, and click the button to pop up the callout, OR click anywhere on the map, nothing happens. If I click again, everything works... Any ideas? I have looked at many theories, but so far no luck...
@IBOutlet weak var mapView: MKMapView!
// In ViewDidLoad, I define my LongPressGestureRecognizer
let uilpgr = UILongPressGestureRecognizer(target: self, action: #selector(createNewAnnotation))
uilpgr.minimumPressDuration = 0.25
mapView.addGestureRecognizer(uilpgr)
// And here is the selector:
@objc func createNewAnnotation(_ sender: UIGestureRecognizer) {
let touchPoint = sender.location(in: self.mapView)
let coordinates = mapView.convert(touchPoint, toCoordinateFrom: self.mapView)
let heldPoint = MKPointAnnotation()
heldPoint.coordinate = coordinates
if (sender.state == .began) {
heldPoint.title = "Set Point"
heldPoint.subtitle = String(format: "%.4f", coordinates.latitude) + "," + String(format: "%.4f", coordinates.longitude)
mapView.addAnnotation(heldPoint)
}
}
If I replace the long press by a tap recognizer, the callout shows up, but the tap creates yet another annotation...so long press has to be the right way. But how can I get around this problem, so that the user can create the annotation with a long press, and then tap once to get the callout??