gpt4 book ai didi

swift - MKDirectionResponse 响应总是返回 nil

转载 作者:搜寻专家 更新时间:2023-11-01 05:43:07 31 4
gpt4 key购买 nike

我正在尝试获取两点之间的路线信息,但是 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")
}
}

最佳答案

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

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

委托(delegate)方法来指定您的覆盖渲染(您的路线在 map 上的外观)。

关于swift - MKDirectionResponse 响应总是返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25454905/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com