Swift中的折线叠加

时间:2014-12-04 23:05:43

标签: 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)

使用此委托方法:

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

self.MapView.addOverlay(polyline)

5 个答案:

答案 0 :(得分:5)

我认为问题在于:

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
}

答案 1 :(得分:1)

您的地图视图似乎已取消分配。折线结构还可以。

通常,变量以小写开头。您是否已将地图视图子类化并尝试访问该类?

答案 2 :(得分:1)

我花了太多时间在WAAAAAAAAYYYY,所以我想我会在类似的问题上添加解决方案。我在带有MKPolygon的addOverlay上得到了一个EXC BAD ACCESS。结果我一直都在错误的线程上。修正了:

var points = [MKMapPoint]()
for var i = 0; i < area.coordinates.count; i+=2 {
  let c = CLLocationCoordinate2DMake(area.coordinates[i], area.coordinates[i+1])
  points.append(MKMapPointForCoordinate(c))
}
let polygon = MKPolygon(points: &points, count: points.count)
dispatch_async(dispatch_get_main_queue(), {
  self.mapView.addOverlay(polygon)
})

答案 3 :(得分:0)

let firstlat : string = "12.9166"
let firstlon : string = "77.6101"

let secondlat : string = "12.9610"
let secondLon : string = "77.6387"

    let point1 = CLLocationCoordinate2DMake(Double(firstlat)!, Double(firstlon)!)
    let point2 = CLLocationCoordinate2DMake(Double(secondlat as String)!, Double(secondLon)!)

    let pickAnnotation : MKPointAnnotation = MKPointAnnotation()
    pickAnnotation.coordinate = point1
    pickAnnotation.title = "pick"
    displayMapView.addAnnotation(pickAnnotation)

    let dropAnnotation : MKPointAnnotation = MKPointAnnotation()
    dropAnnotation.coordinate = point2
    dropAnnotation.title = "drop"
    displayMapView.addAnnotation(dropAnnotation)
    displayMapView.showAnnotations(displayMapView.annotations, animated: true)

    var points: [CLLocationCoordinate2D]
    points = [point1, point2]
   routeLine = MKPolyline(coordinates: &points[0] , count: 2)
    displayMapView.add(routeLine)

答案 4 :(得分:0)

    func showRouteOnMap(_ pickCoordinate: CLLocationCoordinate2D, _ destinationCoordinate: CLLocationCoordinate2D) {

    let request = MKDirections.Request()

    let sourcePlacemark = MKPlacemark(coordinate: pickCoordinate)
    let sourceMapItem = MKMapItem(placemark: sourcePlacemark)

    request.source = sourceMapItem

    let myPlacemark = MKPlacemark(coordinate: destinationCoordinate)
    let destinationMapItem = MKMapItem(placemark: myPlacemark)
    request.destination = destinationMapItem

    request.requestsAlternateRoutes = false

    let directions = MKDirections(request: request)

    directions.calculate(completionHandler: {(response, error) in

        if let error = error {
            print(error.localizedDescription)
        } else {
            if let response = response {
                self.showRoute(response)
            }
        }
    })

}

func showRoute(_ response: MKDirections.Response) {

    for route in response.routes {
        routeMap.addOverlay(route.polyline,
                     level: MKOverlayLevel.aboveRoads)
        self.routeMap.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)
    }

}

// MARK: - MKMapViewDelegate

func mapView(_ mapView:MKMapView,rendererFor overlay:MKOverlay)-> MKOverlayRenderer {

    let renderer = MKPolylineRenderer(overlay: overlay)

    renderer.strokeColor = UIColor(red: 17.0/255.0, green: 147.0/255.0, blue: 255.0/255.0, alpha: 1)

    renderer.lineWidth = 5.0

    return renderer
}