iOS 在 程序內(nèi)調(diào)用手機(jī)上安裝的地圖軟件進(jìn)行導(dǎo)航

// 需求是需要用戶 能從 所在位置 到 附近的健身房的 路線, 然而,就一個(gè)需求,不值當(dāng)?shù)奶砑右粋€(gè)地圖, 就用調(diào)用手機(jī)上第三方地圖軟件, ?什么高德, 百度, 騰訊, iOS 原生地圖都可以, 如果手機(jī)上什么地圖都沒有, 還有原生的 ?地圖, 如果連原生的地圖都刪除了, ?呵呵, ?那我就不管了, ?開玩笑, 如果連原生地圖都刪除了, 會(huì)跳轉(zhuǎn)到 ?appStore 下載, 蘋果自帶的軟件, 不會(huì)刪除, 只是把圖標(biāo)隱藏而已, 下載非常快, 幾秒即可. 不影響什么

// 進(jìn)入正題

// 首先 添加白名單, 這是 必須的, 否則不能跳轉(zhuǎn), 添加在這里

LSApplicationQueriesSchemes

baidumap//百度

iosamap// 高德

comgooglemaps//谷歌


// 谷歌經(jīng)我親測, 在中國已廢, ?打開沒有內(nèi)容, 不用也罷


重點(diǎn)是打開的代碼

iOS 原生地圖

//當(dāng)前的位置

MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];

//目的地的位置

MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude) addressDictionary:nil]];

toLocation.name = name;

NSArray *items = [NSArray arrayWithObjects:currentLocation, toLocation, nil];

NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES };

//打開蘋果自身地圖應(yīng)用,并呈現(xiàn)特定的item

[MKMapItem openMapsWithItems:items launchOptions:options];


// 百度地圖

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {

UIAlertAction * baiduAction = [UIAlertAction actionWithTitle:@"百度地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=%@&mode=driving&coord_type=gcj02",latitude, longitude,name]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

}];

}


// 高德地圖

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {

UIAlertAction * gaodeAction = [UIAlertAction actionWithTitle:@"高德地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",@"一七健康",@"yiqihealth",latitude, longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

}];

}


運(yùn)行之后, 完美

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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