Last active
August 1, 2018 19:42
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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