Detecting touches on MKOverlay in iOS7 (MKOverlayRenderer)

前端 未结 8 1800
灰色年华
灰色年华 2020-12-04 21:58

I have an MKMapView with possibly hundreds of polygons drawn. Using MKPolygon and MKPolygonRenderer as one is suppose to on iOS7.

What I need is a way of acting upon

相关标签:
8条回答
  • 2020-12-04 22:56

    FOR SWIFT 2.1 Find a point/coordinate in a Polygon

    Here is the logic, without tap gestures, to find an annotation inside a polygon.

     //create a polygon
    var areaPoints = [CLLocationCoordinate2DMake(50.911864, 8.062454),CLLocationCoordinate2DMake(50.912351, 8.068247),CLLocationCoordinate2DMake(50.908536, 8.068376),CLLocationCoordinate2DMake(50.910159, 8.061552)]
    
    
    func addDriveArea() {
        //add the polygon
        let polygon = MKPolygon(coordinates: &areaPoints, count: areaPoints.count)
        MapDrive.addOverlay(polygon) //starts the mapView-Function
    }
    
      func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer! {
    
    
        if overlay is MKPolygon {
    
            let renderer = MKPolygonRenderer(overlay: overlay)
            renderer.strokeColor = UIColor.blueColor()
            renderer.lineWidth = 2 
    
            let coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(50.917627), longitude: CLLocationDegrees(8.069562))
    
            let mappoint = MKMapPointForCoordinate(coordinate)
            let point = polygonView.pointForMapPoint(mappoint)
            let mapPointAsCGP = CGPointMake(point.x, point.y);
    
            let isInside = CGPathContainsPoint(renderer.path, nil, mapPointAsCGP, false)
    
            print("IsInside \(isInside)") //true = found
    
            return renderer
        } else {
            return nil
        }
    }
    
    0 讨论(0)
  • 2020-12-04 23:02

    I'm considering to use both overlay and pin Annotation. I get the touch from the pin associated to the overlay.

    0 讨论(0)
提交回复
热议问题