在 iOS 6 之前,打开这样的 URL 会打开 (Google) map 应用:
NSURL *url = [NSURL URLWithString:@"http://maps.google.com/?q=New+York"];
[[UIApplication sharedApplication] openURL:url];
现在有了新的 Apple Maps 实现,这只是将 Mobile Safari 打开到 Google Maps。如何使用 iOS 6 完成相同的行为?如何以编程方式打开 map 应用并让它指向特定的位置/地址/搜索/其他?
最佳答案
这是 Apple 的官方方式:
// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
// Create an MKMapItem to pass to the Maps app
CLLocationCoordinate2D coordinate =
CLLocationCoordinate2DMake(16.775, -3.009);
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate
addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:@"My Place"];
// Pass the map item to the Maps app
[mapItem openInMapsWithLaunchOptions:nil];
}
如果您想获得前往该地点的驾车或步行指示,您可以添加 mapItemForCurrentLocation
与 MKMapItem
在 +openMapsWithItems:launchOptions:
的数组中,并适本地设置启动选项。
// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
// Create an MKMapItem to pass to the Maps app
CLLocationCoordinate2D coordinate =
CLLocationCoordinate2DMake(16.775, -3.009);
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate
addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:@"My Place"];
// Set the directions mode to "Walking"
// Can use MKLaunchOptionsDirectionsModeDriving instead
NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeWalking};
// 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];
}
您可以在 else
中保留原始 iOS 5 及更低版本的代码之后的声明if
.请注意,如果您颠倒 openMapsWithItems:
中的项目顺序,数组,您将获得从坐标到您当前位置的路线。您可以通过传递构造的 MKMapItem
来使用它来获取任意两个位置之间的方向。而不是当前位置 map 项。我没试过。
最后,如果您有一个地址(作为一个字符串)希望得到指示,请使用地理编码器创建一个 MKPlacemark
, 通过 CLPlacemark
.
// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@"Piccadilly Circus, London, UK"
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];
}];
}
https://stackoverflow.com/questions/12504294/
相关文章:
ios - 何时使用 dequeueReusableCellWithIdentifier 与 deq
objective-c - 从 NSString 中删除除数字之外的所有数字
objective-c - cocoa objective-c类中变量前面的下划线如何工作?
iphone - 如何将 NSDictionary 转换为 NSData,反之亦然?
ios - 更改 UIImageView 的图像时淡入淡出/溶解
objective-c - 仅在 iOS 7 上运行时,Storyboard 原型(prototyp
ios - 如何在 UITableViewCell 之间添加间距
ios - Xcode 调试器不打印对象并显示 nil,当它们不是时