问题
I have an app in which I allow users to launch the Maps App (Google or Apple) to view an address.
I used to do this:
Address *address = [self.person.addresses objectAtIndex:0];
NSString *addressString = [NSString stringWithFormat:@"%@ %@ %@ %@, %@ %@",
address.line1 == nil ? @"" : address.line1,
address.line2 == nil ? @"" : address.line2,
address.line3 == nil ? @"" : address.line3,
address.city == nil ? @"" : address.city,
address.state == nil ? @"" : address.state,
address.zip == nil ? @"" : address.zip];
NSString *mapsURL = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", [addressString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mapsURL]];
But to support iOS 6, I've changed it to this:
Address *address = [self.person.addresses objectAtIndex:0];
NSString *addressString = [NSString stringWithFormat:@"%@ %@ %@ %@, %@ %@",
address.line1 == nil ? @"" : address.line1,
address.line2 == nil ? @"" : address.line2,
address.line3 == nil ? @"" : address.line3,
address.city == nil ? @"" : address.city,
address.state == nil ? @"" : address.state,
address.zip == nil ? @"" : address.zip];
// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
/*
// Create an MKMapItem to pass to the Maps app
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:nil
addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem openInMapsWithLaunchOptions:nil];
*/
// I want something like this:
MKMapItem *mapItem = [[MKMapItem alloc] initWithAddressQuery:addressString];
[mapItem openInMapsWithLaunchOptions:nil];
}
else
{
NSString *mapsURL = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", [addressString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mapsURL]];
}
Basically, with the raw address (no ABPersonRef
's or anything) I need to open the location in Apple Maps. Google used to do this just fine.
I've tried the simple switch from maps.google.com
to maps.apple.com
but in iOS 5 it opens to Google Maps web app -- which I don't want! There's a perfectly good native app.
回答1:
Found the answer here. It is accomplished by using the CLGeocoder
class:
// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:addressString
completionHandler:^(NSArray *placemarks, NSError *error) {
// Convert the CLPlacemark to an MKPlacemark
// Note: There's no error checking for a failed geocode
CLPlacemark *geocodedPlacemark = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc]
initWithCoordinate:geocodedPlacemark.location.coordinate
addressDictionary:geocodedPlacemark.addressDictionary];
// Create a map item for the geocoded address to pass to Maps app
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:geocodedPlacemark.name];
// Set the directions mode to "Driving"
// Can use MKLaunchOptionsDirectionsModeWalking instead
NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving};
// Get the "Current User Location" MKMapItem
MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];
// Pass the current location and destination map items to the Maps app
// Set the direction mode in the launchOptions dictionary
[MKMapItem openMapsWithItems:@[currentLocationMapItem, mapItem] launchOptions:launchOptions];
}];
}
//iOS 4/5:
else
{
NSString *mapsURL = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", [addressString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
...
来源:https://stackoverflow.com/questions/13535027/how-to-launch-ios-maps-app-with-specific-address-in-ios-6