0

I have my MKMapViewDelegate in place. Also, 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)

With this Delegate Method:

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
}

I get this: EXC BAD ACCESS Thread 8 on

self.MapView.addOverlay(polyline)
4

5 回答 5

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
}
于 2014-12-05T13:50:39.207 回答
1

我花了太多时间在这上面,所以我想我会为类似的问题添加解决方案。我在带有 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)
})
于 2015-01-14T09:34:29.550 回答
1

您的地图视图似乎已被释放。折线构造正常。

通常,变量以小写字母开头。您是否对地图视图进行了子类化并尝试访问该类?

于 2014-12-04T23:18:57.673 回答
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
}
于 2019-01-09T07:17:20.967 回答
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)
于 2017-03-09T06:47:25.323 回答