-->

iPhone SDK 6 Launching maps with voice navigation

2019-02-07 17:01发布

问题:

I am trying to launch the maps app from my iPhone SDK app. Right now I can launch the maps app with directions but it goes to an overview of the directions and doesn't use Siri and the voice navigation to give turn by turn directions.

currently I have a button that launches this code...

NSString *address = viewedObject.addressFull;
NSString *url = [NSString stringWithFormat: @"http://maps.apple.com/maps?saddr=%f,%f&daddr=%@", here.latitude, here.longitude, [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

回答1:

With iOS 6 there's a new way to launch maps, using openMapsWithItems: in MKMapItem. Here's a snippet that I use that provides walking or driving directions from current location to the provided coordinates:

// iOS 6.0+ only
MKPlacemark* destPlace = [[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil] autorelease];
MKMapItem* destMapItem = [[[MKMapItem alloc] initWithPlacemark:destPlace] autorelease]; destMapItem.name = stationItem.title;

NSArray* mapItems = [[[NSArray alloc] initWithObjects: destMapItem, nil] autorelease];
NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:
                                 walking ? MKLaunchOptionsDirectionsModeWalking : MKLaunchOptionsDirectionsModeDriving,
                                 MKLaunchOptionsDirectionsModeKey, nil];
[MKMapItem openMapsWithItems:mapItems launchOptions:options];

The way you are doing it, which you still have to do if running on pre-iOS 6 devices, you need to include the dirflg in the URL to request walking or driving directions:

// pre iOS 6 code
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirflg=%c",
    currentLocation.coordinate.latitude,
    currentLocation.coordinate.longitude,
    destination.coordinate.latitude,
    destination.coordinate.longitude,
    walking ? 'w' : 'd'];


回答2:

I built ontop of progrmr's answer above...the code below will take a NSString input of an address then forward Geocode it and then open the Maps app with voice navigated directions to the NSString input. The NameString and PhoneString get attached to the placemark put on the Maps app. The code below would not be possible with out progrmr's code above, please mark his answer as useful.

    [self.geocoder geocodeAddressString:AddressString completionHandler:^(NSArray *placemarks, NSError *error) {

        if ([placemarks count] > 0) {
            CLPlacemark *placemark = [placemarks objectAtIndex:0];
            CLLocation *location = placemark.location;
            CLLocationCoordinate2D there = location.coordinate;

            MKPlacemark *destPlace = [[MKPlacemark alloc] initWithCoordinate:there addressDictionary:nil];
            MKMapItem *destMapItem = [[MKMapItem alloc] initWithPlacemark:destPlace];
            destMapItem.name = NameString;
            destMapItem.phoneNumber = PhoneString;

            NSArray* mapItems = [[NSArray alloc] initWithObjects: destMapItem, nil];
            NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsDirectionsModeKey, nil];
            [MKMapItem openMapsWithItems:mapItems launchOptions:options];
        }
    }];