Swift中的折线叠加

Swift中的折线叠加

我的mkmappviewdelegate已经就位。另外,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)

使用此委托方法:
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]

在这里,您直接创建数组,而不指定其类型。
请参阅下面的参考代码以创建多段线覆盖和相关的委托方法:
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)
})

呈现覆盖的委托方法:
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/

10-09 02:17