I have an MKMapView with possibly hundreds of polygons drawn. Using MKPolygon and MKPolygonRenderer as one is suppose to on iOS7.
What I need is a way of acting upon
FOR SWIFT 2.1 Find a point/coordinate in a Polygon
Here is the logic, without tap gestures, to find an annotation inside a polygon.
//create a polygon
var areaPoints = [CLLocationCoordinate2DMake(50.911864, 8.062454),CLLocationCoordinate2DMake(50.912351, 8.068247),CLLocationCoordinate2DMake(50.908536, 8.068376),CLLocationCoordinate2DMake(50.910159, 8.061552)]
func addDriveArea() {
//add the polygon
let polygon = MKPolygon(coordinates: &areaPoints, count: areaPoints.count)
MapDrive.addOverlay(polygon) //starts the mapView-Function
}
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer! {
if overlay is MKPolygon {
let renderer = MKPolygonRenderer(overlay: overlay)
renderer.strokeColor = UIColor.blueColor()
renderer.lineWidth = 2
let coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(50.917627), longitude: CLLocationDegrees(8.069562))
let mappoint = MKMapPointForCoordinate(coordinate)
let point = polygonView.pointForMapPoint(mappoint)
let mapPointAsCGP = CGPointMake(point.x, point.y);
let isInside = CGPathContainsPoint(renderer.path, nil, mapPointAsCGP, false)
print("IsInside \(isInside)") //true = found
return renderer
} else {
return nil
}
}
I'm considering to use both overlay and pin Annotation. I get the touch from the pin associated to the overlay.