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];
    }






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章