MKMapView 中的叠加 MKPolyLine 问题(rendererForOverLay)

Overlay MKPolyLine issue in MKMapView (rendererForOverLay)

我正在尝试在 MapView 上为从位置 A 到位置 B(这是当前用户的 GPS 位置)的路线添加折线,这样 route/polyline 就可以从设定的位置跟随A 到用户当前在(位置 B)的任何地方。

目前我的叠加层无法正常工作。我查看了 MKPolylineView 上的其他几个 SO 线程,但是当我尝试实现它们的代码(也在下面)时,route/lines 仍然没有显示。我是 iOS 的新手,所以我自己还在熟悉 Swift/mapKit。

这是我目前的情况:(已修改以显示重要部分)

class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var setLocButton: UIButton!
@IBOutlet weak var mapView: MKMapView!

var locationManager: CLLocationManager = CLLocationManager()
var carLat = 36.136111, carLong = -80.279462


func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [CLLocation]!) { //AnyObject->CLLocation
    var latestLocation: CLLocation = locations[locations.count - 1]

    //Displaying location A on mapView
    let carCoords = CLLocationCoordinate2D(latitude: carLat, longitude: carLong)
    let region = MKCoordinateRegion(center: carCoords, span: MKCoordinateSpan(latitudeDelta: 0.0035, longitudeDelta: 0.0035))
    mapView.mapType = MKMapType.Hybrid
    mapView.setRegion(region, animated: true)

    //Attempting to display route information on mapView
    mapView.showsUserLocation = true
    var locations = [CLLocation(latitude: carLat, longitude: carLong), CLLocation(latitude: latestLocation.coordinate.latitude, longitude: latestLocation.coordinate.latitude)]
    var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate})
    var polyline = MKPolyline(coordinates: &coordinates, count: locations.count)

    self.mapView.addOverlay(polyline)

}

//rendererForOverlay
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
    if overlay is MKPolyline {

        /* Does not get called */
        print("rendererForOverlay") 

        var polylineRenderer = MKPolylineRenderer(overlay: overlay)
        polylineRenderer.strokeColor = UIColor.blueColor()
        polylineRenderer.lineWidth = 5
        return polylineRenderer
    }
    return nil
}

我还发现了another example using MKDirections,这似乎更理想,因为它允许我设置传输类型(MKDirectionsTransportType.Walking)。不过,我在使用这些说明绘制路线时也遇到了问题。

使用第二组指令,这是我在解决一些错误后得到的 Xcode 提醒我:

var route: MKRoute?

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [CLLocation]!) { //AnyObject->CLLocation
    var latestLocation: CLLocation = locations[locations.count - 1] //AnyObject

    /* 
    ...
    */

    //carLocation = Location A; currentLocation = location B
    var directionsRequest = MKDirectionsRequest()
    let carLocation = MKPlacemark(coordinate: carCoords, addressDictionary: nil) 
    var currentLocation = MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: coorLat, longitude: coorLong), addressDictionary: nil)

    directionsRequest.source = MKMapItem(placemark: currentLocation)
    directionsRequest.destination = MKMapItem(placemark: carLocation)
    directionsRequest.transportType = MKDirectionsTransportType.Walking

    var directions = MKDirections(request: directionsRequest)
    directions.calculateDirectionsWithCompletionHandler {
        (response, error) -> Void in
        if error == nil {
            self.route = response!.routes[0] as? MKRoute
            self.mapView.addOverlay((self.route?.polyline)!)
        }
    }
}

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

    /* Does not get executed as well */
    print("RendererForOverLay")

    var myLineRenderer = MKPolylineRenderer(polyline: (route?.polyline)!)
    myLineRenderer.strokeColor = UIColor.redColor()
    myLineRenderer.lineWidth = 3
    return myLineRenderer
}

我是否以某种方式将 rendererForOverlay 链接错了,因为在这两种情况下都没有调用它?

Sofacoder 是对的,折线渲染器现在可以工作了!

我忘记在我的函数中添加myMap.delegate = self,并且还省略了MKMapViewDelegate ViewController的声明:

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {