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

时间:2015-05-14 21:24:26

标签: ios swift mkmapview mkoverlay

我正在尝试将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
        }
    }

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

1 个答案:

答案 0 :(得分:2)

问题是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之外,以便其他人可以看到它:

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! {
    ...
}