MKDirectionResponse响应始终返回nil

时间:2014-08-22 20:11:41

标签: swift mapkit

我正在尝试获取两点之间的路由信息​​,但MKDirectionResponse总是在控制台上打印nil。我想我错过了一些明显的东西。我在堆栈上寻找类似的问题,但无济于事。

import Foundation
import MapKit
import CoreLocation
import UIKit

class GetDirections: UIViewController, CLLocationManagerDelegate{

    @IBOutlet var mapView: MKMapView!

    var userLatitude: CLLocationDegrees = 0.0
    var userLongitude: CLLocationDegrees = 0.0

    override func viewDidLoad(){
        super.viewDidLoad()

        var locationManager = CLLocationManager()
        var destinationCoordinates:CLLocationCoordinate2D
        =CLLocationCoordinate2DMake(39.9532293,-75.194119);

        locationManager.requestAlwaysAuthorization()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = 30        
        locationManager.startUpdatingLocation()

        self.userLatitude = 40.759211
        self.userLongitude = -73.984638
        var userLocation:CLLocationCoordinate2D =        
        CLLocationCoordinate2DMake(self.userLatitude, self.userLongitude)
        var sourcePlacemark:MKPlacemark = MKPlacemark(coordinate: userLocation,              addressDictionary: nil)
        var destinationPlacemark:MKPlacemark = MKPlacemark(coordinate: destinationCoordinates, addressDictionary: nil)
        var source:MKMapItem = MKMapItem(placemark: sourcePlacemark)
        var destination:MKMapItem = MKMapItem(placemark: destinationPlacemark)
        var directionRequest:MKDirectionsRequest = MKDirectionsRequest()

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

        var directions:MKDirections = MKDirections(request: directionRequest)
        directions.calculateDirectionsWithCompletionHandler({
            (response: MKDirectionsResponse!, error: NSError?) in
            if error != nil{
                println("Error")
            }
            if response != nil{
                println(response.description)
            }
            else{
                println("No response")
            }
            println(error?.description)
        })


        println("ok \(userLatitude), \(userLongitude)")
    }

    override func didReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
    }

    func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]){
        if locations != nil{
            var currentLocation:CLLocation = locations.last as CLLocation
            userLatitude = currentLocation.coordinate.latitude
            userLongitude = currentLocation.coordinate.longitude
            println("ok \(userLatitude), \(userLongitude)")
        }
        else{
            println("Location object is nil")
        }
    }

    func locationManager(manager:CLLocationManager, didFailWithError error: NSError){
        println("Error")
    }
}

1 个答案:

答案 0 :(得分:0)

我认为你需要将MKMapView Delegate添加到你的类并实现

mapView(:MKMapView, rendererForOverlay : MKOverlay) -> MKOverlayRenderer

委托方法来指定叠加渲染(路线在地图上的外观)。