使用 mapKit 的地图显示相反的方向 (iOS Swift)

Maps using mapKit displays the opposite directions (iOS Swift)

我正在尝试使用 Apple 地图显示从用户当前位置到之前固定位置的说明。这是我的代码...

这里我在didUpdateLocations方法中存储了一个名为carInitalLocation和carInitialCoordinate的全局变量作为初始坐标。

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)
            }
        }

一切正常,但问题是它不是将方向从当前位置映射到初始固定位置,而是将其从初始固定位置映射到当前位置,这与我想要的相反。

如有任何帮助,我们将不胜感激。

您可以像这样切换 mapItems 中对象的顺序

let mapItems = [currentLocMapItem, startingLocationItem]

或者您可以将呼叫更改为 openMapsWithItems(_:launchOptions:) 以仅使用一项。

MKMapItem.openMapsWithItems([startingLocationItem], launchOptions:launchOptions)

根据 MKMapItem Class Reference,

If you specify the MKLaunchOptionsDirectionsModeKey option in the launchOptions dictionary, the mapItems array must have no more than two items in it. If the array contains one item, the Maps app generates directions from the user’s current location to the location specified by the map item. If the array contains two items, the Maps app generates directions from the location of the first item to the location of the second item in the array.

但是,您似乎想要指定 MKLaunchOptionsDirectionsModeKey 选项,以便“地图”显示步行路线。因此,您应该选择第一种解决方案。