iOS開發(fā)筆記 調(diào)起本地地圖導(dǎo)航(百度、高德、騰訊、蘋果自帶)

地圖

從自己的APP跳轉(zhuǎn)到用戶本地的APP進行導(dǎo)航。首先,要先查看用戶都安裝了哪些地圖類APP.

下面分3種情況進行分析:
1、用戶沒有安裝第三方的地圖,只有蘋果自帶的地圖應(yīng)用。
2、用戶安裝一款第三方地圖應(yīng)用。
3、用戶安裝了超過一款地圖應(yīng)用。
遇到第1、2情況直接跳轉(zhuǎn)應(yīng)用。第三種情況,需要彈出選項,讓用戶自主選擇。

代碼:

//查看線路
- (void)clickLine:(UIButton *)sender{
    NSMutableArray *mapArr = [NSMutableArray arrayWithCapacity:0];
    
    if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]){
        [mapArr addObject:@"百度地圖"];
    }
    if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]){
        [mapArr addObject:@"高德地圖"];
    }
    if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]){
        [mapArr addObject:@"騰訊地圖"];
    }
    
    if (mapArr.count == 1) {
        [self JumpToMap:mapArr[0]];
    }else if(mapArr.count > 0){
        UIAlertController *mapAlert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        
        for (NSString *mapName in mapArr) {
            UIAlertAction *Action = [UIAlertAction actionWithTitle:mapName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self JumpToMap:action.title];
            }];
            [mapAlert addAction:Action];
        }
        
        //取消
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [mapAlert addAction:cancelAction];
        
        [self presentViewController:mapAlert animated:YES completion:^{
            
        }];
    }else{
        //使用自帶地圖
        [self JumpToMap:@"蘋果地圖"];
    }
}

分流函數(shù):

//選擇地圖
- (void)JumpToMap:(NSString *)mapName{
    if ([mapName isEqualToString:@"蘋果地圖"]) {
        [self appleMap];
    }else if ([mapName isEqualToString:@"百度地圖"]){
        [self BaiduMap];
    }else if ([mapName isEqualToString:@"高德地圖"]){
        [self iosMap];
    }else if ([mapName isEqualToString:@"騰訊地圖"]){
        [self qqMap];
    }
}
備注:如果兼容ios10以下的應(yīng)用,跳轉(zhuǎn)請做好適配,ios10以下系統(tǒng)請使用:
- openURL:(NSURL*)url

百度地圖

百度地圖官方文檔

//百度地圖
- (void)BaiduMap{
    float shopLat = 百度坐標(biāo);
    float shoplng = 百度坐標(biāo);
    
    NSString *urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&mode=transit&coord_type= bd09ll",self.userLocation.location.coordinate.latitude, self.userLocation.location.coordinate.longitude];
    
    if (shopLat != 0 && shoplng != 0) {
        urlString = [NSString stringWithFormat:@"%@&destination=latlng:%f,%f|name:%@", urlString, shopLat, shoplng, @"目標(biāo)地址,你可以自行替換"];
    }else{
        urlString = [NSString stringWithFormat:@"%@&destination=%@|name:%@",urlString, _orderModel.addressStr,_orderModel.addressStr];
    }
    
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
        
    }];
}

高德地圖

高德地圖官方文檔

//高德地圖
- (void)iosMap{
    CLLocationCoordinate2D gcj02Coord = CLLocationCoordinate2DMake(百度坐標(biāo), 百度坐標(biāo));
    
    float shopLat = gcj02Coord.latitude;
    float shoplng = gcj02Coord.longitude;
    
    NSString *urlString = [NSString stringWithFormat:@"iosamap://path?sourceApplication=jikexiue&backScheme=jkxe&slat=%f&slon=%f&sname=我的位置&dev=1&t=1",self.userLocation.location.coordinate.latitude, self.userLocation.location.coordinate.longitude];
    
    if (shopLat != 0 && shoplng != 0) {
        urlString = [NSString stringWithFormat:@"%@&dlat=%f&dlon=%f&dname=%@", urlString, shopLat, shoplng ,_orderModel.addressStr];
    }else{
        urlString = [NSString stringWithFormat:@"%@&dname=%@",urlString, _orderModel.addressStr];
    }
    
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
        
    }];
}

騰訊地圖

騰訊地圖官方文檔

//騰訊地圖
- (void)qqMap{
    
    CLLocationCoordinate2D gcj02Coord = CLLocationCoordinate2DMake(百度坐標(biāo), 百度坐標(biāo));
    
    float shopLat = gcj02Coord.latitude;
    float shoplng = gcj02Coord.longitude;
    
    NSString *urlString = [NSString stringWithFormat:@"qqmap://map/routeplan?type=bus&fromcoord=%f,%f&from=我的位置&referer=jikexiu",self.userLocation.location.coordinate.latitude, self.userLocation.location.coordinate.longitude];
    
    urlString = [NSString stringWithFormat:@"%@&tocoord=%f,%f&to=%@",urlString, shopLat, shoplng, _orderModel.addressStr];
    
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
        
    }];
}

蘋果原生地圖

1、添加庫MapKit.framework
2、引入: #import <MapKit/MapKit.h>

//蘋果原生地圖
- (void)appleMap{
    CLLocationCoordinate2D desCoordinate = CLLocationCoordinate2DMake(_orderModel.lat, _orderModel.lng);
    
    MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
    currentLocation.name = @"我的位置";
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:desCoordinate addressDictionary:nil]];
    toLocation.name = [NSString stringWithFormat:@"%@",_orderModel.addressStr];
    
    [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
                   launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeTransit,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,638評論 4 61
  • 今天有個外賣的app要加導(dǎo)航去商戶地址的功能,就簡單的寫了下調(diào)用目前用的比較廣泛的三個地圖的一鍵導(dǎo)航,還是一如既往...
    Mr在水一方閱讀 13,997評論 0 0
  • 我們的社會階層表面上是用官級大小和財富多少來劃分的,而實際上是用人的“格局”來劃分的。 劉邦要的是天下。 他可以降...
    甲坤閱讀 658評論 0 1
  • 《無印良品管理筆記》這本書主要是講松井忠三的自我回顧,書中他毫無保留的分享了他憑借怎樣的管理方式帶領(lǐng)無印良品...
    Winni0914閱讀 3,677評論 0 1
  • 文/劉晚楓 人和人的相遇有個時間問題,相遇太早,不懂珍惜相互傷害;相遇太晚,心存希望不能得到;相遇這件事情,隱藏著...
    劉晚楓閱讀 915評論 1 9

友情鏈接更多精彩內(nèi)容