我必须绘制一个具有两个角度的圆弧,圆心点和半径作为输入。我使用 UIBezierPath,但叠加层未添加到 map 中。这是我的代码:
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)
}
和我的 map View :
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
,因为几周前我设法画了一个圆弧,但是坐标错误(可以记住我是怎么做到的)。
最佳答案
关于ios - swift:如何通过 MapKit 在 map 上绘制弧线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39979453/