我有一个应用程序,允许用户启动地图应用程序(谷歌或苹果)来查看地址。
我曾经这样做:
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]];
但为了支持 iOS 6,我将其更改为:
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]];
}
基本上,使用原始地址(没有ABPersonRef
's 或任何东西)我需要在 Apple Maps 中打开该位置。谷歌曾经这样做很好。
我尝试了简单的从maps.google.com
to切换,maps.apple.com
但在 iOS 5 中它打开到 Google Maps 网络应用程序——我不想要!有一个非常好的原生应用程序。