用过微信的应该都知道这个功能,对方发一个位置给你,点进去地图展示对方跟你的当前位置,界面提供了选择各个地图应用进行导航,更好这次需求也要搞这个功能。
这个功能实现不难,个人感觉比应用内嵌入第三方地图导航SDK用起来更舒服,更接地气,逼格也高点,主要要的是简单(毕竟人家啥都处理好了)。
上项目效果图(还是想吐槽下傻逼的产品,为啥不直接照搬微信的界面,至少比自己的好看)


这里有一个重点,主要是弹出的选择框的选项并不是固定的,而是根据你手机上有没有安装这个地图应用,没有就不会出现。
这里就要用到
- - (BOOL)canOpenURL:(NSURL *)url NS_AVAILABLE_IOS(3_0);
判断手机上有没有安装该地图应用。
所以要知道地图应用的url Scheme。
这里提供了几个常用地图应用的url Scheme:
- baidumap
- iosamap
- comgooglemaps
- qqmap
- ….
苹果地图不需要,因为它是苹果地图啊,这样也好,能保证没有安装其他地图app,至少还有一个苹果地图,而且苹果地图在IOS9也做的越来越好了,本身API提供了一个跳转打开方法。
这里插入一个小细节,在IOS9之后,苹果进一步完善了安全机制,必须在plist里面设置url scheme白名单,不然无法打开对应的应用

前方高能,重点来了!!!!
这里我抽了个方法返回支持导航的地图信息数组:
- #pragma mark - 导航方法
- - (NSArray *)getInstalledMapAppWithEndLocation:(CLLocationCoordinate2D)endLocation
- {
- NSMutableArray *maps = [NSMutableArray array];
-
-
- NSMutableDictionary *iosMapDic = [NSMutableDictionary dictionary];
- iosMapDic[@"title"] = @"苹果地图";
- [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=latlng:%f,%f|name=北京&mode=driving&coord_type=gcj02",endLocation.latitude,endLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- baiduMapDic[@"url"] = urlString;
- [maps addObject:baiduMapDic];
- }
-
-
- if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
- NSMutableDictionary *gaodeMapDic = [NSMutableDictionary dictionary];
- gaodeMapDic[@"title"] = @"高德地图";
- NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",@"导航功能",@"nav123456",endLocation.latitude,endLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- gaodeMapDic[@"url"] = urlString;
- [maps addObject:gaodeMapDic];
- }
-
-
- if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
- NSMutableDictionary *googleMapDic = [NSMutableDictionary dictionary];
- googleMapDic[@"title"] = @"谷歌地图";
- NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",@"导航测试",@"nav123456",endLocation.latitude, endLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- googleMapDic[@"url"] = urlString;
- [maps addObject:googleMapDic];
- }
-
-
- if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
- NSMutableDictionary *qqMapDic = [NSMutableDictionary dictionary];
- qqMapDic[@"title"] = @"腾讯地图";
- NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&tocoord=%f,%f&to=终点&coord_type=1&policy=0",endLocation.latitude, endLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- qqMapDic[@"url"] = urlString;
- [maps addObject:qqMapDic];
- }
-
- return maps;
- }
这里只要传入提供的坐标点,就打包好了需要的信息。
什么,你说弹出?那玩意自己去写,不想写,自己去找,不然就用系统的actionSheet。
- #pragma mark LCActionSheetDelegate
- -(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:self.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];
- }
最后结尾还有个小插曲,由于我们后台给的目标经纬度是百度坐标系,项目用的也是百度地图,界面上展示是没啥问题了,但是由于不同的地图有自己的坐标系,而要用它们进行导航,那传给它们的必须是标准的经纬度坐标,这就蛋疼了,百度这坑爹只有提供了标准的转成它自己的坐标系,而没有提供反转的方法,去官网开发者社区发帖,也没用,只是回答你没有提供,所以我猜传言百度地图SDK是实习生写的还是有一定的可靠性。
没办法,只能求爷爷告奶奶,去查找对应的资料,还是找到一个第三方的转换方法https://github.com/JackZhouCn/JZLocationConverter,但是仍然有一定的偏差,能接受的就接受吧。