ios项目中调用百度、高德、本机地图导航
发布时间
阅读量:
阅读量
最近开发过程中遇到一个要求必须直接调用接口实现的问题,在此提供代码方案:无需依赖官方提供的SDK框架。
百度地图
第一步 准备工作
调用百度地图导航先需要导入以下几个包:

在项目中会依赖百度地图的两大框架来实现相关的功能定位需求。获取链接请参考以下地址:http://lbsyun.baidu.com/index.php?title=iossdk/sdkiosdev-download。
另外提供了两份百度地图导航功能的.ico文件资源,在这里你可以找到具体的下载链接:http://lbsyun.baidu.com/index.php?title=ios-navsdk/sdkios-nav-download。
然后剩下的只需自行添加到building phases中,不要遗漏了
第二步 添加info
在Info区域,在位于LSApplicationQueriesSchemes字段组下的位置新增Baidu地图具体内容请参考下图

第三步 代码部分
在需要调用的.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];
}
全部评论 (0)
还没有任何评论哟~
