2

I am trying to draw a semi-sphere on MKMapView knowing the center coordinates, start and end angles, and radius in nautical miles.

Using this thread(How to draw UIBezierPath overlay on MKMapView?), I have subclassed MKOverlayPathRenderer to draw an arc:

import UIKit
import MapKit
class IGAAcarsDrawArc: MKOverlayPathRenderer
{
    let PI = 3.14159265
    let radius : CGFloat = 10.0
    var startAngle: CGFloat = 0
    var endAngle: CGFloat = 3.14159
    var latitude = 25.96728611
    var longitude = -80.453019440000006

    override func createPath()
    {
        let line = MKPolyline()

        let arcWidth: CGFloat = 5

        let path = UIBezierPath(arcCenter: CGPointMake(CGFloat(latitude), CGFloat(longitude)),
                                radius: self.radius,
                                startAngle: startAngle,
                                endAngle: endAngle,
                                clockwise: true)

        path.lineWidth = arcWidth
        path.stroke()
    }
}

Now, it is not clear how do I use this to create MKPolyline and implement in mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay).

This thread (How to draw arc/curve line with MKOverlayView on MKMapView) does not shed too much light on the issue either.

Can someone please help draw an arc in MKMapView?

EDIT:

This is not working:

func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer
{
   if overlay is IGAAcarsDrawArc
        {
            let arcLine = IGAAcarsDrawArc(overlay: overlay)

            arcLine.lineWidth = 8
            arcLine.strokeColor = UIColor.magentaColor()
        }


        return MKPolylineRenderer()
 }

Thanks a lot!

Community
  • 1
  • 1
Igor Tupitsyn
  • 1,193
  • 3
  • 18
  • 45
  • I found http://stackoverflow.com/questions/12062902/how-to-add-a-uibezierpath-to-a-mkmapview-using-overlays Maybe it will give you some hints – wenqi Jun 25 '16 at 03:29
  • I found this post: http://stackoverflow.com/questions/12062902/how-to-add-a-uibezierpath-to-a-mkmapview-using-overlays Maybe it will give you some hints – wenqi Jun 25 '16 at 03:34

1 Answers1

0

You should not draw the path, but assign it to the path property of the renderer:

Subclasses should override it and use it to create the CGPathRef data type to be used for drawing. After creating the path, your implementation should assign it to the path property.

So

override func createPath()
{
…
  let path = UIBezierPath(arcCenter: CGPointMake(CGFloat(latitude), CGFloat(longitude)),
                            radius: self.radius,
                            startAngle: startAngle,
                            endAngle: endAngle,
                            clockwise: true)

  path.lineWidth = arcWidth
  self.path = path.cgPath // assign instead of stroke
}

Typed in Safari.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
  • Thank you. However, I have already tried it. I believe it does not work for UIBezierPath since the following error is shown: "Value of type 'UIBezierPath' has no member 'cgPath'. – Igor Tupitsyn Jun 25 '16 at 15:23
  • Of course, it is `CGPath`. Typo. However, if this is the error, simply create an instance of `CGPath` directly. – Amin Negm-Awad Jun 25 '16 at 16:33