swift:如何通过MapKit在地图上绘制圆弧?

时间:2016-10-11 14:21:35

标签: ios swift mapkit uibezierpath

我必须画一个有两个角度的圆弧,中心点和半径作为输入。我使用UIBezierPath,但叠加没有添加到地图中。这是我的代码:

func calculateByArc()
    {
        let startingAzimuth = Double(upperLimitTextBox.text!)
        let endingAzimuth = Double(upperLimitUomTextBox.text!)
        let radius = Double(lowerLimitTextBox.text!)
        let latitude = Double(lowerLimitUomTextBox.text!)
        let longitude = Double(textField5.text!)
        let clockwise = clockwiseSwitch.isOn ? true : false
        let qwe = CLLocationCoordinate2DMake(latitude!,longitude!)
        let asd = Map.convert(qwe, toPointTo: Map)
        let test = Map.convert(asd, toCoordinateFrom: Map)
        print("test lat: " + String(test.latitude))
        print("test lon: " + String(test.longitude))


        let path = UIBezierPath(arcCenter: Map.convert (qwe, toPointTo: mapController.Map), radius: CGFloat(radius!), startAngle: CGFloat(Helper.toRad(startingAzimuth!)), endAngle: CGFloat(Helper.toRad(endingAzimuth!)) , clockwise: clockwise)
        let arc = MKCircle(center: CLLocationCoordinate2DMake(latitude!, longitude!), radius: radius!)
        arc.accessibilityPath = path
        Map.add(arc)
    }

和我的mapView:

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer
    {
        if overlay is MKCircle
        {
            print("overlay latitude: "+String(overlay.coordinate.latitude))
            print("overlay longitude: "+String(overlay.coordinate.longitude))
            let circleOverlay = overlay as! MKCircle
            if(circleOverlay.accessibilityPath != nil)
            {
                let arcRenderer = MKOverlayPathRenderer()
                arcRenderer.path = circleOverlay.accessibilityPath?.cgPath
                arcRenderer.strokeColor = UIColor.red
                arcRenderer.lineWidth = 10
                arcRenderer.alpha = 0.3
                return arcRenderer
            }
            let circle = MKCircleRenderer(overlay: overlay)
            circle.strokeColor = UIColor.black
            circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1)
            circle.lineWidth = 1
            circle.alpha = 0.3
            return circle
        }
}

我想这是因为我没有正确地将CLLocationCoordinate2D转换为CGPoint,因为几周前我设法绘制了一个圆弧,但坐标错误(不记得我是怎么做的)这一点)。

1 个答案:

答案 0 :(得分:2)