iPhone SDK 6 Launching maps with voice navigation directions

强颜欢笑 提交于 2019-12-18 13:38:54

问题


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];
        }
    }];


来源:https://stackoverflow.com/questions/13893093/iphone-sdk-6-launching-maps-with-voice-navigation-directions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!