【发布时间】:2015-07-09 06:00:43
【问题描述】:
我正在尝试使用 Apple 地图显示从用户当前位置到之前固定位置的说明。这是我的代码...
这里我存储了一个名为 carInitalLocation 的全局变量和 carInitialCoordinate 作为 didUpdateLocations 方法中的初始坐标。
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
// Find location of user
var userLocation:CLLocation = locations[0] as! CLLocation
var latitude = userLocation.coordinate.latitude
var longitude = userLocation.coordinate.longitude
var latDelta:CLLocationDegrees = 0.1
var longDelta: CLLocationDegrees = 0.1
var span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
var location = CLLocationCoordinate2DMake(latitude, longitude)
var region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
var coordinate:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude);
carInitialLocation = userLocation;
carInitialCoordinate = coordinate;
self.map.setRegion(region, animated: true)
manager.stopUpdatingLocation()
}
然后我想找到当前位置,启动苹果地图并提供返回原始固定位置的路线和路线。我尝试使用下面的代码来做到这一点:
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
let selectedLoc = view.annotation
if(control == view.rightCalloutAccessoryView) {
println(view.annotation.title) // annotation's title
println(view.annotation.subtitle)
println("Annotation '\(selectedLoc.title!)' has been selected")
performSegueWithIdentifier("detailViewSegue", sender: self)
} else {
if(control == view.leftCalloutAccessoryView) {
let currentLocMapItem = MKMapItem.mapItemForCurrentLocation()
let selectedPlacemark = MKPlacemark(coordinate: selectedLoc.coordinate, addressDictionary: nil)
let selectedMapItem = MKMapItem(placemark: selectedPlacemark);
let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeWalking]
var placemarkStartingLocation:MKPlacemark = MKPlacemark(coordinate: carInitialCoordinate, addressDictionary: nil)
var startingLocationItem:MKMapItem = MKMapItem(placemark: placemarkStartingLocation);
let mapItems = [startingLocationItem, currentLocMapItem]
MKMapItem.openMapsWithItems(mapItems, launchOptions:launchOptions)
}
}
一切正常,但问题是,它不是将方向从当前位置映射到初始固定位置,而是将其从初始固定位置映射到当前位置,这与我想要的相反。
任何帮助将不胜感激。
【问题讨论】: