Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift: how to draw an arc on map via MapKit?

I have to draw an arc having two angles, center point and radius as input. I use UIBezierPath, but overlay isn't added to the map. Here's my code:

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)
    }

and my 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
        }
}

I guess it's because i don't properly convert CLLocationCoordinate2D to CGPoint, because a couple of weeks ago i managed to draw an arc, but with wrong coordinates(can't remember how i did that).

like image 520
Jamil Avatar asked Oct 17 '25 23:10

Jamil


1 Answers

Is this what you are looking for http://nshipster.com/mkgeodesicpolyline/ ?

like image 98
slonkar Avatar answered Oct 19 '25 12:10

slonkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!