detect if a point is inside a MKPolygon overlay

后端 未结 6 1966
余生分开走
余生分开走 2020-12-07 23:44

I want to be able to tell if tap is within a MKPolygon.

I have a MKPolygon:

CLLocationCoordinate2D  points[4];

points[0] = CLLocationCoordinate2DMak         


        
6条回答
  •  爱一瞬间的悲伤
    2020-12-08 00:06

    This worked for me in #Swift 4.2:

    extension MKPolygon {
        func isCoordinateInsidePolyon(coordinate: CLLocationCoordinate2D) -> Bool {
            let polygonRenderer = MKPolygonRenderer(polygon: self)
            let currentMapPoint: MKMapPoint = MKMapPoint(coor)
            let polygonViewPoint: CGPoint = polygonRenderer.point(for: currentMapPoint)
            if polygonRenderer.path == nil {
                return false
            }else{
                return polygonRenderer.path.contains(polygonViewPoint)
            }
        }
    }
    

提交回复
热议问题