跳转第三方地图进行导航

在iOS开发过程中,有可能会遇到需要导航的情况出现,这是我们可以调用第三方App进行导航,简单方便,也显得高大上。最近公司在开发地图是遇到这方面的内容,特记录一下,如果给您带来便利,请点赞。谢谢🙏🙏🙏🙏🙏🙏

 

跟据得到的地理位置得到经纬度

  CLGeocoder *myGeocoder = [[CLGeocoder alloc] init];

    [myGeocoder geocodeAddressString:obj.address completionHandler:^(NSArray *placemarks, NSError *error) {

        if ([placemarks count] > 0 && error == nil) {

            NSLog(@"Found %lu placemark(s).", (unsigned long)[placemarks count]);

            CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];

            CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude;

            CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude;

            CLLocationCoordinate2D endLocation=CLLocationCoordinate2DMake(latitude, longitude);

            self.maps=[self getInstalledMapAppWithEndLocation:endLocation AMapCloudPOI:obj];

            /**

             *  是否安装 高德地图

             */

            if (self.maps.count>0) {

                UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"选择地图" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles: nil];

                for (NSDictionary *dic in self.maps)

                {

                    [action addButtonWithTitle:dic[@"title"]];

                }

                [action showInView:self.view];

            }

            

            else

            {

                

                PilotViewController *pilotVC=[[PilotViewController alloc]init];

                pilotVC.fromLocation=self.location;

                pilotVC.adress=obj.address;

                pilotVC.name=obj.name;

                [self.navigationController pushViewController:pilotVC animated:YES];

            }

        }

        else if ([placemarks count] == 0 && error == nil) {

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"系统提示" message:@"地理位置不准确" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];

            [alert show];

            return;

        } else if (error != nil) {

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"系统提示" message:@"地理位置不准确" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];

            [alert show];

            return;

        }

    }];

 

 

首先检测用户安装了哪些导航,这里我屏蔽了苹果自带的导航。

- (NSArray *)getInstalledMapAppWithEndLocation:(CLLocationCoordinate2D)endLocation AMapCloudPOI:(AMapCloudPOI*)obj
{
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,obj.name] 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",@"导航功能",@"AOZ移动平台",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",@"导航测试",@"AOZ移动平台",obj.name,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,obj.name] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
qqMapDic[@"url"] = urlString;
[maps addObject:qqMapDic];
}

return maps;
}

点击UIActionSheet判断选中的是那个App ,并进行跳转。

#pragma mark UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    if ([actionSheet.title isEqualToString:@"选择地图"])

    {

        

        //        if (buttonIndex==nil) {

        //            return;

        //        }

        //       else

        if (buttonIndex > 0) {

            

            NSDictionary *dic = self.maps[buttonIndex-1];

            NSString *urlString = dic[@"url"];

            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

        }

    }

}

 不出意外的话。应该可以做到,第一次分享,欢迎交流

posted on 2017-10-27 10:57  索忆mcc  阅读(2138)  评论(0)    收藏  举报

导航