How to draw an arc in MKMapView?

Hello. I got another question.


I need to draw half-circle on MKMapView knowing the center coordinates, start and end angles, and radius in nautical miles.


I have subclassed MKOverlayPathRenderer:


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). Does anyone know how to draw an arc in MKMapView?


Thanks a lot!

Replies

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

I see this post is six years old...but...here's how I did it:

func addPolyLine(coordArray: [CLLocationCoordinate2D], count: Int, name: String){
    
    let polyline = MKPolyline(coordinates: coordArray, count: count)
    polyline.title = name
    self.cruiseMap.delegate = self
    self.cruiseMap.addOverlay(polyline)
}



func addSemiCircle(centerCoord: CLLocationCoordinate2D, r: Double, startBearing: Double, title: String){ // r in feet, bearings in degrees
    
    var dots: [CLLocationCoordinate2D] = [N777.location, N777.location,N777.location,N777.location,N777.location,N777.location,N777.location,N777.location, N777.location,N777.location,N777.location,N777.location,N777.location] 

// N777 is a structure, one of its variables is "location" which is a CLLocationCoordinate2D

    let radianInc = 15*degreesToRadians
    
    for dot in 0...12{
        
        dots[dot] = locationWithBearing(bearing: startBearing*degreesToRadians + (Double(dot)*radianInc), distanceMeters: r*feetToMeters, origin: centerCoord)
    }
    
    addPolyLine(coordArray: dots, count: 13, name: title)
}

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    
    if let polyline = overlay as? MKPolyline {
        let lineRenderer = MKPolylineRenderer(polyline: polyline)
        lineRenderer.alpha = 1.0
        lineRenderer.strokeColor = polyLineColor
        lineRenderer.lineWidth = polyLineWidth
    return lineRenderer
    }
    

    
return MKOverlayRenderer()
}