简体   繁体   中英

Maps using mapKit displays the opposite directions (iOS Swift)

I'm trying to display instructions using Apple maps from a user's current location to a location that was previously pinned. Here is my code...

Here I store a global variable called carInitalLocation and carInitialCoordinate as the initial coordinate in the didUpdateLocations method.

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

}

I then want to find the current location, launch apple maps and provide directions and route back to the original pinned location. I attempt to do this with the code below:

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

Everything works but the problem is that instead of mapping directions from the current location to the initial pinned location, it maps it from the initial pinned location to the current location, which is opposite as to what I want.

Any help would be greatly appreciated.

You could switch the order of the objects in mapItems like so

let mapItems = [currentLocMapItem, startingLocationItem]

Or you could change your call to openMapsWithItems(_:launchOptions:) to use only one item.

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

According to the 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.

However, it seems that you want to specify the MKLaunchOptionsDirectionsModeKey option so Maps displays walking directions. Therefore, you should opt for the first solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM