最近写项目遇到不加官方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];
}