最近寫項目遇到不加官方SDK,而直接調接口的要求,話不多說直接上代碼:
百度地圖
第一步 準備工作
第二步 添加info
第三步 代碼部分
#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
第三步 代碼部分
#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];
}