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

时间:2015-07-09 06:00:44

标签: ios swift maps mapkit

我尝试使用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)
            }
        }

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

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

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

let mapItems = [currentLocMapItem, startingLocationItem]

或者您可以将通话更改为openMapsWithItems(_:launchOptions:),只使用一个项目。

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

根据MKMapItem Class Reference

  

如果在launchOptions字典中指定MKLaunchOptionsDirectionsModeKey选项,则mapItems数组中的项目必须不超过两个。如果数组包含一个项目,则地图应用程序会生成从用户当前位置到地图项目指定位置的路线。如果数组包含两个项目,则地图应用程序会生成从第一个项目的位置到阵列中第二个项目的位置的路线。

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