應(yīng)用跳轉(zhuǎn)百度地圖或本機(jī)地圖導(dǎo)航

項(xiàng)目截圖

最近項(xiàng)目要求苔埋,詳情頁有一個(gè)公司的地址贱迟。點(diǎn)擊地址那一欄塞关,會(huì)跳出來這個(gè)彈出框,跳到相應(yīng)地圖頁面進(jìn)行導(dǎo)航抬探,起點(diǎn)就是用戶當(dāng)前位置,終點(diǎn)就是詳情頁這個(gè)地址帆赢,高德和百度默認(rèn)起點(diǎn)是用戶當(dāng)前位置小压,目的地需要將詳情頁地址轉(zhuǎn)為經(jīng)緯度傳過去,話不多說匿醒,直接上代碼~

1.彈出框

 UIAlertController  *alertCon = [UIAlertController  alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

        UIAlertAction *AlertAction1 = [UIAlertAction  actionWithTitle:@"高德地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self  zhuan];
            
        }];
        
        UIAlertAction *AlertAction2 = [UIAlertAction  actionWithTitle:@"百度地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
            [self baidu];
            
        }];
        
        
        UIAlertAction *AlertAction3 = [UIAlertAction  actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertCon  addAction:AlertAction1];
        
         [alertCon  addAction:AlertAction2];
        
        [alertCon  addAction:AlertAction3];
        
        [self  presentViewController:alertCon animated:YES completion:nil];

2.跳轉(zhuǎn)到高德地圖场航,需要傳目的地的經(jīng)緯度和名字

//將填寫的地址信息拿過來轉(zhuǎn)為經(jīng)緯度
-(void)zhuan{
    
    //1.獲得輸入的地址
    NSString *address = self.model.address;
        if (address.length==0) return;
    
         //2.開始地理編碼
         //說明:調(diào)用下面的方法開始編碼,不管編碼是成功還是失敗都會(huì)調(diào)用block中的方法
        [self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
                //如果有錯(cuò)誤信息廉羔,或者是數(shù)組中獲取的地名元素?cái)?shù)量為0溉痢,那么說明沒有找到
                 if (error || placemarks.count==0) {
                      // self.detailAddressLabel.text=@"你輸入的地址沒找到,可能在月球上";
                    }else   //  編碼成功憋他,找到了具體的位置信息
                        {
                                 //打印查看找到的所有的位置信息
                                   /*
                                        61                     name:名稱
                                        62                     locality:城市
                                        63                     country:國家
                                        64                     postalCode:郵政編碼
                                        65                  */
                                for (CLPlacemark *placemark in placemarks) {
                                        NSLog(@"name=%@ locality=%@ country=%@ postalCode=%@",placemark.name,placemark.locality,placemark.country,placemark.postalCode);
                                     }
                   
                                 //取出獲取的地理信息數(shù)組中的第一個(gè)顯示在界面上
                                CLPlacemark *firstPlacemark=[placemarks firstObject];
                                 //詳細(xì)地址名稱
                              // self.detailAddressLabel.text=firstPlacemark.name;
                               //緯度
                              CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude;
                                //經(jīng)度
                                CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude;
                            
                            NSLog(@"%f %f",latitude,longitude);
                            
                            coords2 = CLLocationCoordinate2DMake(latitude,longitude);
                            
                            Xlat =  latitude;
                            
                            Xlong = longitude;
                            
                            [self  gaode];
                            
                           }
           }];

}

-(void)gaode{
    
    //當(dāng)前的位置 //
    
    //    MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
    
    //起點(diǎn)
    
    //MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coords1 addressDictionary:nil]];
    
    //目的地的位置
    
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coords2 addressDictionary:nil]];
    
    toLocation.name = self.model.address;
    
    //currentLocation.name = locationName;
    
    NSArray *items = [NSArray arrayWithObjects: toLocation, nil];
    
    NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES }; //打開蘋果自身地圖應(yīng)用孩饼,并呈現(xiàn)特定的item
    
    [MKMapItem openMapsWithItems:items launchOptions:options];


}

3.跳轉(zhuǎn)百度,需要傳目的地的經(jīng)緯度和名字

-(void)baidu{
    
    
    BOOL hasBaiduMap = NO;
    
    BOOL hasGaodeMap = NO;
    
    if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){
        
        hasBaiduMap = YES;
        
        NSLog(@"yesbaidu");
        
    }else
        
    {
        
        NSLog(@"nobaidu");
        
    }
    
    if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"iosamap://"]]){
        
        hasGaodeMap = YES;
        
    }
    
    //NSString *url2 = [[NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:39.915,116.404|name:我的位置&destination=latlng:39.915,120.202|name:白楊路199弄&mode=driving"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
    
    NSString  *url2 = [[NSString  stringWithFormat:@"baidumap://map/direction?origin=latlng:%f|name:我的位置&destination=latlng:%f|name:%@&mode=driving", Xlat, Xlong,self.model.address]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
    
    
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:url2]];

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市竹挡,隨后出現(xiàn)的幾起案子镀娶,更是在濱河造成了極大的恐慌,老刑警劉巖揪罕,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件梯码,死亡現(xiàn)場離奇詭異,居然都是意外死亡好啰,警方通過查閱死者的電腦和手機(jī)轩娶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來框往,“玉大人鳄抒,你說我怎么就攤上這事∫祝” “怎么了许溅?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長秉版。 經(jīng)常有香客問我贤重,道長,這世上最難降的妖魔是什么沐飘? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任游桩,我火速辦了婚禮牲迫,結(jié)果婚禮上耐朴,老公的妹妹穿的比我還像新娘借卧。我一直安慰自己,他們只是感情好筛峭,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布铐刘。 她就那樣靜靜地躺著,像睡著了一般影晓。 火紅的嫁衣襯著肌膚如雪镰吵。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天挂签,我揣著相機(jī)與錄音疤祭,去河邊找鬼。 笑死饵婆,一個(gè)胖子當(dāng)著我的面吹牛勺馆,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播侨核,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼草穆,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了搓译?” 一聲冷哼從身側(cè)響起悲柱,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎些己,沒想到半個(gè)月后豌鸡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡段标,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年涯冠,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片怀樟。...
    茶點(diǎn)故事閱讀 39,926評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡功偿,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出往堡,到底是詐尸還是另有隱情械荷,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布虑灰,位于F島的核電站吨瞎,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏穆咐。R本人自食惡果不足惜颤诀,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一字旭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧崖叫,春花似錦遗淳、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至脂男,卻和暖如春养叛,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背宰翅。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工弃甥, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人汁讼。 一個(gè)月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓淆攻,卻偏偏與公主長得像,于是被迫代替她去往敵國和親掉缺。 傳聞我的和親對象是個(gè)殘疾皇子卜录,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內(nèi)容