ios项目中调用百度、高德、本机地图导航

最近写项目遇到不加官方SDK,而直接调接口的要求,话不多说直接上代码:

百度地图

第一步 准备工作

调用百度地图导航先需要导入以下几个包:

其中需要用到百度地图的两个framework,链接在这:http://lbsyun.baidu.com/index.php?title=iossdk/sdkiosdev-download
还有百度导航的文件两个.a文件,链接在这:http://lbsyun.baidu.com/index.php?title=ios-navsdk/sdkios-nav-download
然后剩下的只需自行添加到building phases中,不要遗漏了

第二步 添加info

在info中在LSApplicationQueriesSchemes下添加baidumap,效果如下图:


第三步 代码部分

在需要调用的.h文件中加入
#import <BaiduMapAPI_Base/BMKBaseComponent.h>
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>

然后调用代码
//判断是否有百度地图
    bool hasBaiduMap = NO;    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        hasBaiduMap = YES;
    }
	if (hasBaiduMap) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提醒" message:@"启动百度地图" preferredStyle:UIAlertControllerStyleAlert];
        
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *baidu = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
              //百度地图
              [self startBaiduMap];
            
        }];
 [alert addAction:cancel];
 [alert addAction:baidu];
 
  [self presentViewController:alert animated:YES completion:nil];
    }
//百度地图
-(void) startBaiduMap
{    
    CLLocationCoordinate2D myLocation = CLLocationCoordinate2DMake(29.569389,106.557978);
    CLLocationCoordinate2D storeLoacation = CLLocationCoordinate2DMake(29.615133,106.605053);
        
    //转换国测局坐标(google地图、soso地图、aliyun地图、mapabc地图和amap地图所用坐标)至百度坐标
    NSDictionary* testdic = BMKConvertBaiduCoorFrom(myLocation,BMK_COORDTYPE_GPS);
    NSDictionary* testdic2 = BMKConvertBaiduCoorFrom(storeLoacation,BMK_COORDTYPE_GPS);
   
    myLocation = BMKCoorDictionaryDecode(testdic);
    storeLoacation = BMKCoorDictionaryDecode(testdic2);
    
    NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:%@&destination=latlng:%f,%f|name:%@&mode=driving", myLocation.latitude, myLocation.longitude, @"我的位置", storeLoacation.latitude, storeLoacation.longitude, @"美心洋人街"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
    
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];
    
}

高德地图

第一步 准备工作

导入所需的两个包:

可以自行去高德地图官网下载这里就不贴链接了,注意只需要这两个包,不用到其他功能

第二步 添加info

在info中在LSApplicationQueriesSchemes下添加iosamap,效果如下图:

第三步 代码部分


#import <AMapFoundationKit/AMapFoundationKit.h>

然后调用代码
//高德地图
-(void) startGaodeMap
{
    CLLocationCoordinate2D myLocation = CLLocationCoordinate2DMake(29.569389,106.557978);
    CLLocationCoordinate2D storeLoacation = CLLocationCoordinate2DMake(29.615133,106.605053);
    myLocation = AMapCoordinateConvert(myLocation,AMapCoordinateTypeGPS);;
    storeLoacation =AMapCoordinateConvert(storeLoacation,AMapCoordinateTypeGPS);;
    
    NSString *urlString = [[NSStringstringWithFormat:@"iosamap://path?sourceApplication=%@&sid=BGVIS1&slat=%f&slon=%f&sname=%@&did=BGVIS2&dlat=%f&dlon=%f&dname=%@&dev=0&m=0&t=0",@"", myLocation.latitude, myLocation.longitude,@"我的位置", storeLoacation.latitude, storeLoacation.longitude, destName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [[UIApplication sharedApplication]openURL:[NSURLURLWithString:urlString]];
}

//判断是否有高德地图
    bool hasGaodeMap = NO;    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        hasGaodeMap = YES;
    }
	if (hasGaodeMap) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提醒" message:@"启动ga地图" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *gaode = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
              //高德地图
              [self startGaodeMap];
            
        }];
 [alert addAction:cancel];
 [alert addAction:gaode];
 
  [self presentViewController:alert animated:YES completion:nil];
    }






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值