MKPolygon - How to determine if a CLLocationCoordinate2D is in a CLLocationCoordinate2D polygon?

前端 未结 5 995
春和景丽
春和景丽 2021-01-15 06:06

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 with

5条回答
  •  攒了一身酷
    2021-01-15 06:38

    You can create a polygon renderer object and check if it's path contains the point based on the location in the map:

    func checkIf(_ location: CLLocationCoordinate2D, areInside polygon: MKPolygon) -> Bool {
        let polygonRenderer = MKPolygonRenderer(polygon: polygon)
        let mapPoint = MKMapPointForCoordinate(location)
        let polygonPoint = polygonRenderer.point(for: mapPoint)
    
        return polygonRenderer.path.contains(polygonPoint)
    }
    

提交回复
热议问题