2016-10-11 132 views
3

我必須繪製具有兩個角度,中心點和半徑作爲輸入的弧。我使用UIBezierPath,但疊加不會添加到地圖中。這裏是我的代碼:SWIFT:如何通過MapKit在地圖上劃出一道弧線?

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

和我的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 
     } 
} 

我想這是因爲我沒有正確地轉換CLLocationCoordinate2DCGPoint,因爲幾個星期前,我成功地劃出一道弧線,但與錯誤的座標(不記得我是如何做到這一點)。

回答

相關問題