使用mapKit的地图显示相反的方向(iOS Swift)
我正在尝试使用Apple地图显示从用户当前位置到以前固定位置的指示。这是我的密码 在这里,我存储了一个名为carinitialocation和carinitialcoordination的全局变量,作为didUpdateLocations方法中的初始坐标使用mapKit的地图显示相反的方向(iOS Swift),ios,swift,maps,mapkit,Ios,Swift,Maps,Mapkit,我正在尝试使用Apple地图显示从用户当前位置到以前固定位置的指示。这是我的密码 在这里,我存储了一个名为carinitialocation和carinitialcoordination的全局变量,作为didUpdateLocations方法中的初始坐标 func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { // Find location of us
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()
}
然后我想找到当前位置,启动apple maps,并提供返回原始固定位置的方向和路线。我尝试使用以下代码执行此操作:
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)
}
}
一切都正常,但问题是,它不是将方向从当前位置映射到初始固定位置,而是将其从初始固定位置映射到当前位置,这与我想要的相反
任何帮助都将不胜感激。您可以像这样在
mapItems
中切换对象的顺序
let mapItems = [currentLocMapItem, startingLocationItem]
或者,您可以将调用更改为openMapsWithItems(\uux0:launchOptions:)
以仅使用一个项目
MKMapItem.openMapsWithItems([startingLocationItem], launchOptions:launchOptions)
据报道,
如果在launchOptions字典中指定mkLaunchOptions Directions ModeKey选项,则mapItems数组中的项不得超过两个。如果数组包含一个项目,则“地图”应用程序将生成从用户当前位置到地图项目指定位置的方向。如果数组包含两个项目,则“地图”应用程序将生成从数组中第一个项目位置到第二个项目位置的方向
但是,似乎要指定mklaunchoptions方向modekey
选项,以便“地图”显示行走方向。因此,您应该选择第一种解决方案