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

前端 未结 5 872
春和景丽
春和景丽 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:40

    Method to be attached to a gesture recognizer:

    @objc
    func mapTapped(_ tap: UILongPressGestureRecognizer) {
    
        if tap.state == .recognized {
            let touchPoint = tap.location(in: mapView)
            let coord = mapView.convert(touchPoint, toCoordinateFrom: mapView)
    
            for overlay: MKOverlay in mapView.overlays {
    
                if let polygon = overlay as? MKPolygon {
                    let renderer = MKPolygonRenderer(polygon: polygon)
                    let mapPoint = MKMapPoint(coord)
                    let rendererPoint = renderer.point(for: mapPoint)
    
                    if renderer.path.contains(rendererPoint) {
                        // here comes your code
                    }
                }
            }
        }
    }
    

提交回复
热议问题