I have the swift code below that draws a polygon and drops an annotation on MKMapView. I am trying to figure out how i could identify if the annotation's coordinate is within the polygon?
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let initialLocation = CLLocation(latitude: 49.140838, longitude: -123.127886)
centerMapOnLocation(initialLocation)
addBoundry()
var annotation = MKPointAnnotation()
annotation.coordinate = point1
annotation.title = "Roatan"
annotation.subtitle = "Honduras"
mapView.addAnnotation(annotation)
}
var points = [CLLocationCoordinate2DMake(49.142677, -123.135139),
CLLocationCoordinate2DMake(49.142730, -123.125794),
CLLocationCoordinate2DMake(49.140874, -123.125805),
CLLocationCoordinate2DMake(49.140885, -123.135214)]
var point1 = CLLocationCoordinate2DMake(49.141821, -123.131577)
func addBoundry() {
let polygon = MKPolygon(coordinates: &points, count: points.count)
mapView.addOverlay(polygon)
}
let regionRadius: CLLocationDistance = 1000
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordinateRegion, animated: true)
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolygon {
let polygonView = MKPolygonRenderer(overlay: overlay)
polygonView.strokeColor = UIColor.magentaColor()
return polygonView
}
return nil
}
}