Determining if a latitude / longitude point is in MKPolygon in Mapview?

At this moment I am trying to find out if the coordinate is in MKMapView inside MKPolygon, drawn ahead of schedule with latitudes / longitudes.

I use CGPathContainsPoint to determine if the coordinate is inside the polygon on the map, but always returns false regardless of the coordinate I selected.

Can someone explain what exactly is going on? Below is my code in Swift.

class ViewController: UIViewController, MKMapViewDelegate @IBOutlet weak var mapView: MKMapView! let initialLocation = CLLocation(latitude: 43.656734, longitude: -79.381576) let point = CGPointMake(43.656734, -79.381576) let regionRadius: CLLocationDistance = 500 let point1 = CLLocationCoordinate2D(latitude: 43.656734, longitude: -79.381576) var points = [CLLocationCoordinate2DMake(43.655782, -79.382094), CLLocationCoordinate2DMake(43.657499, -79.382310), CLLocationCoordinate2DMake(43.656656, -79.380497), CLLocationCoordinate2DMake(43.655782, -79.382094)] override func viewDidLoad() { super.viewDidLoad() centerMapOnLocation(initialLocation) let polygon = MKPolygon(coordinates: &points, count: points.count) mapView.addOverlay(polygon) var annotation = MKPointAnnotation() annotation.coordinate = point1 annotation.title = "Test" annotation.subtitle = "Test" mapView.addAnnotation(annotation) self.mapView.delegate = self } 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.redColor() if CGPathContainsPoint(polygonView.path, nil, CGPointMake(43.656734, -79.381576), true) { print("True!!!!!") } else { println("False") } return polygonView } return nil } 
+5
source share
2 answers

You confuse CGPoints, which are x and y coordinate pairs that indicate a location in your view, with CLLocationCoordinate2Ds, which are coordinates on Earth.

For CGPathContainsPoint, you want to pass your polygonRenderer path (created from your polygon to viewDidLoad ()) and a current location like this:

 let polygonRenderer = MKPolygonRenderer(polygon: polygon) let currentMapPoint: MKMapPoint = MKMapPointForCoordinate(locationManager.location?.coordinate) let polygonViewPoint: CGPoint = polygonRenderer.pointForMapPoint(currentMapPoint) if CGPathContainsPoint(polygonRenderer.path, nil, polygonViewPoint, true) { print("Your location was inside your polygon.") } 

So, you want to think about where you should put this code, since where you are right now, inside the function that is called when MKOverlay is drawn on the screen, and this is completely unrelated to this.

+3
source

Updated for Swift 3

 let polygonRenderer = MKPolygonRenderer(polygon: polygon) let mapPoint: MKMapPoint = MKMapPointForCoordinate(coordinate) let polygonViewPoint: CGPoint = polygonRenderer.point(for: mapPoint) if polygonRenderer.path.contains(polygonViewPoint) { print("Your location was inside your polygon.") } 
+3
source

All Articles