我正在尝试将路线从用户的位置写到目的地点。
我在info.plist
这些行添加
这是我编写路线的代码:
class ItinerarioVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet var steps: UITextView!
@IBOutlet var distanceLabel: UILabel!
@IBOutlet var destinationLabel: UILabel!
@IBOutlet var theMapView: MKMapView!
let locationManager = CLLocationManager()
var annotationTitle: NSString!
var annotationSubheading:Int!
var routeDetails: MKRoute!
var allSteps: NSString!
var destinationCoordinate: CLLocationCoordinate2D!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
//setup our location manager
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
//setup our map view
theMapView.showsUserLocation = true
var destinationPlacemark:MKPlacemark = MKPlacemark(coordinate: destinationCoordinate, addressDictionary: nil)
var source:MKMapItem = MKMapItem.mapItemForCurrentLocation()
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 for getting direction: \(error)")
}
if response != nil{
// find the best way
var temp:MKRoute = response.routes.first as MKRoute
var min = response.routes.first?.distance
for (var i=1; i < response.routes.count; i++)
{
if response.routes[i].distance < min
{min = response.routes[i].distance
temp = response.routes[i] as MKRoute}
}
self.routeDetails = temp
self.theMapView.addOverlay(self.routeDetails.polyline)
if (self.annotationSubheading != 0)
{
if self.annotationSubheading == 20
{
self.destinationLabel.text = eventPub.title as String
}
else
{
self.destinationLabel.text = companyWithServicePub.name as String
}
self.destinationLabel.adjustsFontSizeToFitWidth = true
}
self.distanceLabel.text = NSString(format:"%0.1f KM", self.routeDetails.distance/1000)
setRegionToFit(self.theMapView)
self.allSteps = ""
for (var i = 0; i < self.routeDetails.steps.count; i++)
{
var step:MKRouteStep = self.routeDetails.steps[i] as MKRouteStep;
var newStep:NSString = step.instructions
self.allSteps = self.allSteps.stringByAppendingString( "\(i+1). ")
self.allSteps = self.allSteps.stringByAppendingString(newStep)
self.allSteps = self.allSteps.stringByAppendingString("\n\n");
self.steps.text = self.allSteps;
}
}
else{
println("No response")
}
println(error?.description)
})
// show destination on map
var destannotation = CustomPointAnnotation()
destannotation.title = annotationTitle
destannotation.coordinate = destinationCoordinate
destannotation.subheading = annotationSubheading
self.theMapView.addAnnotation(destannotation)
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolyline {
var polylineRenderer = MKPolylineRenderer(overlay: overlay)
polylineRenderer.strokeColor = UIColor.blueColor()
polylineRenderer.lineWidth = 4
return polylineRenderer
}
return nil
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks,error) -> Void in
if (error != nil) {
println("Error to get user location: " + error.localizedDescription)
return
}
if placemarks.count > 0 {
let pm = placemarks[0] as CLPlacemark
}
else {println("Error with data")}
})
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("Error: with user location" + error.localizedDescription)
self.locationManager.stopUpdatingLocation()
}
}
我尝试在我的iphone ios 8.1.2上运行此功能,允许&#34;您的应用程序&#34;在用户显示应用消息时访问您的位置并且我出现此错误,只有当我首先手动转到设置/隐私/为我的应用设置允许地理位置时我才不会任何错误:
Error for getting direction: Optional(Error Domain=MKLocationErrorDomain Code=0 "The operation couldn’t be completed. (MKLocationErrorDomain error 0.)" UserInfo=0x17086ee40 {NSLocalizedRecoverySuggestion=Active localization to allow (null) to determine your location})
No response
Optional("Error Domain=MKLocationErrorDomain Code=0 \"The operation couldn’t be completed. (MKLocationErrorDomain error 0.)\" UserInfo=0x17086ee40 {NSLocalizedRecoverySuggestion=Active localization to allow (null) to determine your location}")
Error: with user locationThe operation couldn’t be completed. (kCLErrorDomain error 1.)
有人能告诉我我的代码有什么问题吗? 我想知道如何让我的应用程序继续找到用户位置,即使第一次尝试没有实现它?
提前谢谢。