我正在构建一个可以打开地图应用的应用,其中包含从用户当前位置到另一个位置的路线。代码如下所示:
- (id)resolveDirectionsFromCoordinate:(CLLocationCoordinate2D)startCoordinate toCoordinate:(CLLocationCoordinate2D)endCoordinate
{
NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f",
startCoordinate.latitude, startCoordinate.longitude,
endCoordinate.latitude, endCoordinate.longitude];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
return nil;
}
这在iOS 5.x中运行良好。但是,在iOS 6中,由于地图不再使用谷歌地图,因此会引入Safari。
有谁知道我应该在iOS 6中调用哪个URL?
答案 0 :(得分:19)
Apple Documentation建议使用等效的maps.apple.com网址计划
所以使用
http://maps.apple.com/maps?saddr=%f,%f&daddr=%f,%f
而不是
http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f
向后兼容您的代码
NSString* versionNum = [[UIDevice currentDevice] systemVersion];
NSString *nativeMapScheme = @"maps.apple.com";
if ([versionNum compare:@"6.0" options:NSNumericSearch] == NSOrderedAscending){
nativeMapScheme = @"maps.google.com";
}
NSString* url = [NSString stringWithFormat: @"http://%@/maps?saddr=%f,%f&daddr=%f,%f", nativeMapScheme startCoordinate.latitude, startCoordinate.longitude,
endCoordinate.latitude, endCoordinate.longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
或者您也可以使用maps://saddr=%f,%f&daddr=%f,%f
方案,但它似乎不支持所有参数。
答案 1 :(得分:8)
使用+ (BOOL)openMapsWithItems:(NSArray *)mapItems launchOptions:(NSDictionary *)launchOptions
方法。来自MKMapItem Class Reference:
您可以使用此方法将一个或多个地图项目传递到地图应用。例如,您可以使用此方法让Google地图应用显示应用生成的基于位置的搜索结果。地图在您指定的每个位置显示引脚,并使用每个地图项目对象的内容显示其他信息。
答案 2 :(得分:2)
// 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 "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];
}
这就是全部:)
答案 3 :(得分:0)
我建议您查看CMMapLauncher,这是我用于启动Apple,Google和其他具有特定映射请求的iOS地图应用程序而构建的迷你库。使用CMMapLauncher,获得问题中指示的代码将是:
[CMMapLauncher launchMapApp:CMMapAppAppleMaps
forDirectionsFrom:[CMMapPoint mapPointWithName:@"Origin"
coordinate:startCoordinate]
to:[CMMapPoint mapPointWithName:@"Destination"
coordinate:endCoordinate]];
正如您所看到的,它还封装了iOS 6和iOS之间所需的版本检查。其他