Skip to content

Instantly share code, notes, and snippets.

@JoshuaKaden
Last active August 1, 2018 19:42
Show Gist options
  • Save JoshuaKaden/fe8a666d58e3b6c488f05e1989717782 to your computer and use it in GitHub Desktop.
Save JoshuaKaden/fe8a666d58e3b6c488f05e1989717782 to your computer and use it in GitHub Desktop.
MapView extension that passes the lat/long of any tap to the delegate
import MapKit
extension MKMapView {
open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
if let touch = touches.first {
if touch.tapCount == 1 {
let touchLocation = touch.location(in: self)
let coordinate = convert(touchLocation, toCoordinateFrom: self)
let fakeAnnotation = FakeAnnotation(coordinate: coordinate)
let identifier = "\(coordinate.latitude),\(coordinate.longitude)"
let fakeView = MKAnnotationView(annotation: fakeAnnotation, reuseIdentifier: identifier)
delegate?.mapView!(self, didSelect: fakeView)
}
}
}
private class FakeAnnotation: NSObject, MKAnnotation {
let coordinate: CLLocationCoordinate2D
init(coordinate: CLLocationCoordinate2D) {
self.coordinate = coordinate
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment