问题
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!
回答1:
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.
来源:https://stackoverflow.com/questions/37801822/swift-draw-a-semi-sphere-in-mkmapview