导航两种实现方式 (集成第三方SDK、URL跳转第三方应用) 项目要求实现直接跳转 所以接下来做的是实现URL跳转第三方地图来导航了
实现如下的效果
1.首先配置URL Scheme 和白名单
百度地图 URL Scheme: baidumap:// 文档链接
高德地图 URL Scheme: iosamap:// 文档链接
谷歌地图 URL Scheme:comgooglemaps:// 文档链接
腾讯地图 URL Scheme:qqmap:// 文档链接
2.根据以下代码检测你手机上有没有安装这个地图应用,没有就不会出现
- (BOOL)canOpenURL:(NSURL*)url NS_AVAILABLE_IOS(3_0);
示例: [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]];
3.在IOS9之后,苹果进一步完善了安全机制,必须在plist里面设置url scheme白名单,不然无法打开对应的应用
3.判断并装入数组
- (NSArray*)getInstalledMapAppWithEndLocation:(CLLocationCoordinate2D)endLocation
{
NSMutableArray*maps = [NSMutableArrayarray];
//苹果地图
NSMutableDictionary*iosMapDic = [NSMutableDictionarydictionary];
iosMapDic[@"title"] =@"苹果地图";
[mapsaddObject:iosMapDic];
//百度地图
if([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"baidumap://"]]) {
NSMutableDictionary*baiduMapDic = [NSMutableDictionarydictionary];
baiduMapDic[@"title"] =@"百度地图";
NSString*urlString = [[NSStringstringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=北京&mode=driving&coord_type=gcj02",endLocation.latitude,endLocation.longitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
baiduMapDic[@"url"] = urlString;
[mapsaddObject:baiduMapDic];
}
//高德地图
if([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"iosamap://"]]) {
NSMutableDictionary*gaodeMapDic = [NSMutableDictionarydictionary];
gaodeMapDic[@"title"] =@"高德地图";
NSString*urlString = [[NSStringstringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",@"导航功能",@"nav123456",endLocation.latitude,endLocation.longitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
gaodeMapDic[@"url"] = urlString;
[mapsaddObject:gaodeMapDic];
}
//谷歌地图
if([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"comgooglemaps://"]]) {
NSMutableDictionary*googleMapDic = [NSMutableDictionarydictionary];
googleMapDic[@"title"] =@"谷歌地图";
NSString*urlString = [[NSStringstringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",@"导航测试",@"nav123456",endLocation.latitude, endLocation.longitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
googleMapDic[@"url"] = urlString;
[mapsaddObject:googleMapDic];
}
//腾讯地图
if([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"qqmap://"]]) {
NSMutableDictionary*qqMapDic = [NSMutableDictionarydictionary];
qqMapDic[@"title"] =@"腾讯地图";
NSString*urlString = [[NSStringstringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&tocoord=%f,%f&to=终点&coord_type=1&policy=0",endLocation.latitude, endLocation.longitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
qqMapDic[@"url"] = urlString;
[mapsaddObject:qqMapDic];
}
returnmaps;
}
4.底部弹窗
UIActionSheet*myActionSheet = [[UIActionSheetalloc]init];
myActionSheet.delegate = self;
[myActionSheetaddButtonWithTitle:@"取消"];
[myActionSheetshowInView:[UIApplicationsharedApplication].keyWindow];
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex != -1) {
if(buttonIndex ==0) {
[self navAppleMap];
return;
}
NSDictionary*dic =self.maps[buttonIndex];
NSString *urlString = dic[@"url"];
[[UIApplication sharedApplication] openURL: [NSURLURL WithString:urlString]];
}
}
//苹果地图
- (void)navAppleMap
{
CLLocationCoordinate2D gps = [JZLocationConverterbd09ToWgs84:self.destinationCoordinate2D];
MKMapItem *currentLoc = [MKMapItemmapItemForCurrentLocation];
MKMapItem*toLocation = [[MKMapItemalloc]initWithPlacemark:[[MKPlacemarkalloc]initWithCoordinate:gpsaddressDictionary:nil]];
NSArray*items = @[currentLoc,toLocation];
NSDictionary*dic = @{
MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),
MKLaunchOptionsShowsTrafficKey : @(YES)
};
[MKMapItemopenMapsWithItems:itemslaunchOptions:dic];
}