I'm drawing overlays based on directions. I draw from route a to route b and then route b to route c.
I would like to detect if the overlay is tapped anywhere on mkoverlay.
I used this example Detecting touches on MKOverlay in iOS7 (MKOverlayRenderer)
and converted it to swift.
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
if touch.tapCount == 1 {
let touchLocation = touch.location(in: self)
let locationCoordinate = self.convert(touchLocation, toCoordinateFrom: self)
let mapPoint: MKMapPoint = MKMapPointForCoordinate(locationCoordinate)
let mapPointAsCGP = CGPoint(x: CGFloat(mapPoint.x), y: CGFloat(mapPoint.y))
for overlay: MKOverlay in self.overlays {
if (overlay is MKPolygon) {
let polygon: MKPolygon? = (overlay as? MKPolygon)
let mpr: CGMutablePath = CGMutablePath()
let polygonPoints = polygon?.points
let polygonPointCount = Int((polygon?.pointCount)!)
for p in 0..<polygonPointCount {
let mp: MKMapPoint = polygonPoints.unsafelyUnwrapped()[polygonPointCount]
if p == 0 {
mpr.move(to: CGPoint(x: CGFloat(mp.x), y: CGFloat(mp.y)), transform: .identity)
}
else {
mpr.addLine(to: CGPoint(x: CGFloat(mp.x), y: CGFloat(mp.y)), transform: .identity)
}
}
if mpr.contains(mapPointAsCGP, using: .winding, transform: .identity) {
print("test")
}
}
}
}
}
super.touchesEnded(touches, with: event)
}
I'm not sure why they have the if statement there
if (overlay is MKPolygon)
because this would never be called since its an array of mkoverlay.