如何在MKMapView上绘制自定义形状

问题描述 投票:0回答:1

我需要绘制自定义形状,例如ArcSemi-circle吗?我尝试了以下代码,但未在MKMapView上呈现任何内容。

这是绘制自定义形状的正确方法吗?

class ViewController: UIViewController {

  @IBOutlet weak var mapView: MKMapView!

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    mapView.delegate = self

    let center = CLLocationCoordinate2D(latitude: 15.463157486154865, longitude: 73.78846049308775)
    let radius = CLLocationCoordinate2D(latitude: 15.495608080208948, longitude: 73.83418584279791)
    addCircle(center: center, radius: radius)
  }

  private func createArcPath() -> UIBezierPath {
    let center = CLLocationCoordinate2D(latitude: 15.463157486154865, longitude: 73.78846049308775)
    // converting the coordinates to CGPoint with respect to MKMapView.
    let centerPoint = mapView.convert(center, toPointTo: self.mapView)
    // Creating bezierPath of arc.
    let path = UIBezierPath(arcCenter: centerPoint, radius: 6080.205481929489, startAngle: CGFloat.pi, endAngle: CGFloat.pi * 2, clockwise: true)
    return path
  }
}

extension ViewController: MKMapViewDelegate {
  func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if overlay is MKCircle {
      let arcRenderer = MKOverlayPathRenderer()
      arcRenderer.path = createArcPath().cgPath
      arcRenderer.strokeColor = UIColor.red
      arcRenderer.fillColor = UIColor.red
      arcRenderer.lineWidth = 10
      arcRenderer.alpha = 1
      arcRenderer.lineCap = .round
      return arcRenderer
    }
    return MKOverlayRenderer()
  }
}

extension ViewController {
  private func addCircle(center ccoordinate: CLLocationCoordinate2D, radius rcoordinate: CLLocationCoordinate2D) {
    let centerLocation = CLLocation.init(latitude: ccoordinate.latitude, longitude: ccoordinate.longitude)
    let radiusLocation = CLLocation.init(latitude: rcoordinate.latitude, longitude: rcoordinate.longitude)
    let radius = centerLocation.distance(from: radiusLocation)
    let circle = MKCircle(center: ccoordinate, radius: radius)

    mapView.addOverlay(circle)
  }
}
swift mkmapview mkoverlay mkoverlaypathrenderer
1个回答
0
投票

使用它在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
        }
}
© www.soinside.com 2019 - 2024. All rights reserved.