ios - Swift 中的折线叠加

标签 ios swift mkmapview mkpolyline

我有我的 MKMapViewDelegate。此外,MapView.delegate = self

let c1 = myCLLocationCoodinate
let c2 = myCLLocationCoodinate2
var a = [c1, c2]
var polyline = MKPolyline(coordinates: &a, count: a.count)
self.MapView.addOverlay(polyline)

使用这个委托(delegate)方法:

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {

    if overlay is MKPolyline {
        var polylineRenderer = MKPolylineRenderer(overlay: overlay)
        polylineRenderer.strokeColor = UIColor.whiteColor()
        polylineRenderer.lineWidth = 2 
        return polylineRenderer
    }
    return nil
}

我明白了:EXC BAD ACCESS Thread 8 on

self.MapView.addOverlay(polyline)

最佳答案

我认为这里的问题在于行:

var a = [c1, c2]

这里你直接创建了数组,没有指定它的类型。

请参阅下面的引用代码来创建 Polyline 叠加层和相关的委托(delegate)方法:

let c1 = myCLLocationCoodinate
let c2 = myCLLocationCoodinate2

var points: [CLLocationCoordinate2D]
points = [c1, c2]

var geodesic = MKGeodesicPolyline(coordinates: &points[0], count: 2)
mapView.add(geodesic)

UIView.animate(withDuration: 1.5, animations: { () -> Void in
    let span = MKCoordinateSpanMake(20, 20)
    let region1 = MKCoordinateRegion(center: c1, span: span)
    mapView.setRegion(region1, animated: true)
})

渲染叠加层的委托(delegate)方法:

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {

   if overlay is MKPolyline {
       var polylineRenderer = MKPolylineRenderer(overlay: overlay)
       polylineRenderer.strokeColor = UIColor.whiteColor()
       polylineRenderer.lineWidth = 2 
       return polylineRenderer
   } 
   return nil
}

关于ios - Swift 中的折线叠加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27305871/

相关文章:

ios - MKMapView检测点击坐标处是否有POI

ios - SpriteKit SKCameraNode - 在分配后展开可选值时为零

objective-c - 如何从 Swift 调用 Objective-C 单例?

iphone - 如何在固定时自动显示地点标题?

ios - 带外部 GPS 的 CLLocationManager/MKMapView

ios - 如何更改导航栏字体

ios - 尝试将值附加到字典

iOS:消息 'frame' 的接收者为零

ios - Swift 以编程方式向 super View 添加约束

ios - 如何取消用户通知