我无法让我的 MKRoute 显示在 MapView 上

I cannot get my MKRoute to show up on a MapView

我正在尝试将 MKOverlay(我的 MKRoute)添加到我的 MapView,但我不知道我做错了什么。这很草率,因为我在我的应用程序中实现它之前正在试验它。

代码如下:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet var map: MKMapView!

    var geocoder:CLGeocoder = CLGeocoder()
    var location:CLLocation = CLLocation(latitude: 38, longitude: -77)
    var next:CLLocation = CLLocation(latitude: 38.9, longitude: -77.61)
    var locMark:MKPlacemark?
    var destMark:MKPlacemark?
    var manager:CLLocationManager = CLLocationManager()
    var source:MKMapItem?
    var destination:MKMapItem?
    var request:MKDirectionsRequest = MKDirectionsRequest()
    var directions:MKDirections = MKDirections()
    var directionsResponse:MKDirectionsResponse = MKDirectionsResponse()
    var route:MKRoute! = MKRoute()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.map.delegate = self
        manager.requestAlwaysAuthorization()

        let latDelt = 0.05
        let lonDelt = 0.05

        let span:MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: latDelt, longitudeDelta: lonDelt)
        let region:MKCoordinateRegion = MKCoordinateRegion(center: CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude), span: span)
        map.setRegion(region, animated: true)


        map.mapType = MKMapType.Satellite


        locMark = MKPlacemark(coordinate: CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude), addressDictionary: nil)
        destMark = MKPlacemark(coordinate: CLLocationCoordinate2DMake(next.coordinate.latitude, next.coordinate.longitude), addressDictionary: nil)

        source = MKMapItem(placemark: locMark)
        destination = MKMapItem(placemark: destMark)

        request.setSource(source)
        request.setDestination(destination)
        request.transportType = MKDirectionsTransportType.Automobile
        request.requestsAlternateRoutes = true

        let directions = MKDirections(request: request)

        directions.calculateDirectionsWithCompletionHandler { (response:MKDirectionsResponse?, error:NSError?) -> Void in

            if error == nil {
                self.directionsResponse = response!
                self.route = self.directionsResponse.routes[0] as! MKRoute
                self.map.addOverlay(self.route.polyline)
            } else {
                println(error)
            }
        }

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

            if overlay is MKPolyline {
                var pr = MKPolylineRenderer(overlay: overlay)
                pr.strokeColor = UIColor.purpleColor()
                pr.lineWidth = 5
                return pr
            }
            return nil
        }
    }

我没有打印出任何错误,但地图上根本没有显示路线。

问题是 rendererForOverlay 函数 嵌套 viewDidLoad:

override func viewDidLoad() {
    //your viewDidLoad code is here...

    //rendererForOverlay is declared here inside viewDidLoad...
    func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
        ...
    }

}  // <-- closing brace of viewDidLoad

因此,rendererForOverlay 函数在地图视图中隐藏,并且不会调用它,导致不显示叠加层。

要解决此问题,请将 rendererForOverlay 函数移到 viewDidLoad 之外,以便它对 class 的其余部分可见:

override func viewDidLoad() {
    //your viewDidLoad code is here...

}  // <-- closing brace of viewDidLoad

//put rendererForOverlay here outside viewDidLoad...
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
    ...
}