Swift:在MKMapView中绘制一个半球形 [英] Swift: Draw a semi-sphere in MKMapView

查看:105
本文介绍了Swift:在MKMapView中绘制一个半球形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在MKMapView上绘制半球,以了解中心坐标,开始和结束角度以及以海里为单位的半径.

I am trying to draw a semi-sphere on MKMapView knowing the center coordinates, start and end angles, and radius in nautical miles.

使用该线程(如何在MKMapView上绘制UIBezierPath叠加层?),我将MKOverlayPathRenderer子类化以绘制圆弧:

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

现在,不清楚如何使用它来创建MKPolyline并在mapView中实现(mapView:MKMapView,rendererForOverlay覆盖:MKOverlay).

Now, it is not clear how do I use this to create MKPolyline and implement in mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay).

此线程(如何绘制弧/曲线在MKMapView上与MKOverlayView对齐的行)也未在此问题上说明太多.

This thread (How to draw arc/curve line with MKOverlayView on MKMapView) does not shed too much light on the issue either.

有人可以帮忙在MKMapView中绘制圆弧吗?

Can someone please help draw an arc in MKMapView?

这不起作用:

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

非常感谢!

推荐答案

您不应绘制路径,而应将其分配给渲染器的path属性:

You should not draw the path, but assign it to the path property of the renderer:

子类应重写它,并使用它创建用于绘制的CGPathRef数据类型.创建路径后,您的实现应将其分配给path属性.

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.

所以

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
}

在Safari中输入

这篇关于Swift:在MKMapView中绘制一个半球形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆