Swift中的折线叠加

BX6*_*X69 0 mkmapview ios mkpolyline swift

我有我的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)
Run Code Online (Sandbox Code Playgroud)

使用此委托方法:

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
}
Run Code Online (Sandbox Code Playgroud)

我明白了:EXC BAD ACCESS线程8开启了

self.MapView.addOverlay(polyline)
Run Code Online (Sandbox Code Playgroud)

Kam*_*pai 5

我认为问题在于:

var a = [c1, c2]
Run Code Online (Sandbox Code Playgroud)

在这里,您直接创建数组而不指定其类型.

请参阅以下参考代码以创建折线叠加层和相关的委托方法:

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)
})
Run Code Online (Sandbox Code Playgroud)

用于渲染叠加层的委托方法:

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
}
Run Code Online (Sandbox Code Playgroud)