项目里面有位置功能,需要有导航,导航两种实现方式 (集成第三方SDK、URL跳转第三方应用) ,直接集成就不说,下面来说下通过url跳转,
最终效果如如下:
如果手机上安装的有客户端就展示,没有就不展示,文档上面写的很详细的,具体地址如下:
高德地图:https://lbs.amap.com/api/amap-mobile/guide/ios/navi
百度地图:http://lbsyun.baidu.com/index.php?title=uri/api/ios
腾讯地图:http://lbs.qq.com/uri_v1/guide-mobile-navAndRoute.html
国内用的暂时就这些,不过需要注意的是,腾讯地图有一个参数需要注册平台开发者账号才能使用,参数还是必传的,如下图:
具体实现代码如下:
一、添加白名单
二、具体实现代码如下:
-(void)getOpenURL
{
[self getInstalledMapAppWithEndLocation:destinationCoordinate2D];
for (int i = 0; i < self.maps.count; i ++) {
NSString *title = [NSString stringWithFormat:@"%@",self.maps[i][@"title"]];
[_titleMaps addObject:title];
}
}
#pragma mark 一键导航
- (IBAction)navigationAction:(id)sender {
// 实例方法
LCActionSheet *sheet = [[LCActionSheet alloc] initWithTitle:@"请选择地图"
buttonTitles:_titleMaps
redButtonIndex:-1
delegate:self];
[sheet show];
}
-(void)actionSheet:(LCActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != -1) {
if (buttonIndex == 0) {
[self navAppleMap];
return;
}
NSDictionary *dic = self.maps[buttonIndex];
NSString *urlString = dic[@"url"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}
}
//苹果地图
- (void)navAppleMap
{
CLLocationCoordinate2D gps = [JZLocationConverter bd09ToWgs84:destinationCoordinate2D];
MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:gps addressDictionary:nil]];
NSArray *items = @[currentLoc,toLocation];
NSDictionary *dic = @{
MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),
MKLaunchOptionsShowsTrafficKey : @(YES)
};
[MKMapItem openMapsWithItems:items launchOptions:dic];
}
#pragma mark - 导航方法
- (NSArray *)getInstalledMapAppWithEndLocation:(CLLocationCoordinate2D)endLocation
{
//苹果地图
NSMutableDictionary *iosMapDic = [NSMutableDictionary dictionary];
iosMapDic[@"title"] = @"苹果地图";
[self.maps addObject:iosMapDic];
//百度地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
NSMutableDictionary *baiduMapDic = [NSMutableDictionary dictionary];
baiduMapDic[@"title"] = @"百度地图";
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin=我的位置&destination=%f,%f&mode=driving&src=webapp.navi.yourCompanyName.yourAppName",endLocation.latitude,endLocation.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
baiduMapDic[@"url"] = urlString;
[self.maps addObject:baiduMapDic];
}
//高德地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
NSMutableDictionary *gaodeMapDic = [NSMutableDictionary dictionary];
gaodeMapDic[@"title"] = @"高德地图";
NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=applicationName&poiname=fangheng&poiid=BGVIS&lat=%f&lon=%f&dev=1&style=2",endLocation.latitude,endLocation.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
gaodeMapDic[@"url"] = urlString;
[self.maps addObject:gaodeMapDic];
}
//腾讯地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
NSMutableDictionary *qqMapDic = [NSMutableDictionary dictionary];
qqMapDic[@"title"] = @"腾讯地图";
NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&from=我的位置&to=洗车点&tocoord=%f,%f&referer=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77",endLocation.latitude,endLocation.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
qqMapDic[@"url"] = urlString;
[self.maps addObject:qqMapDic];
}
return self.maps;
}
然后直接调用getOpenURL方法即可,传经纬度是用CLLocationCoordinate2D直接传的,展示用的是大神写好的第三方库:LCActionSheet,上面的代码可以直接使用,亲测有效!如果变动,请以文档为准!
作者:稻草人11223