很多小伙伴在做項目中都會用到導航功能,這一期就為大家帶來快速集成導航的方法,這裡使用的是百度導航(優點:集成方便算路快,不用跳轉,滿滿的套路,CV即可)。
導入百度SDK,手動添加依賴庫
配置.plist文件
HelloWorld-5.png//***百度地圖***//官網注冊申請密鑰key [BNCoreServices_Instance initServices:@"lKWaL65uOvOBZ5rw50XA7Ukv5HhSVQHm"]; [BNCoreServices_Instance startServicesAsyn:nil fail:nil];
####用起點坐標和終點坐標計算路徑
#pragma mark 發起導航算路(起點和重點都放在這個方法裡計算規劃路徑) - (void)startCalculateNavi { //***節點數組*** NSMutableArray *nodesArray = [[NSMutableArray alloc] initWithCapacity:2]; //***起點*** //獲得當前定位 CLLocation *myLocation=[BNCoreServices_Location getLastLocation]; BNRoutePlanNode *startNode=[[BNRoutePlanNode alloc] init]; startNode.pos=[[BNPosition alloc] init]; startNode.pos.x=myLocation.coordinate.longitude; startNode.pos.y=myLocation.coordinate.latitude; startNode.pos.eType=BNCoordinate_OriginalGPS; //模擬定位點 // BNRoutePlanNode *startNode = [[BNRoutePlanNode alloc] init]; // startNode.pos = [[BNPosition alloc] init]; // startNode.pos.x = 113.936392; // startNode.pos.y = 22.547058; ![Uploading baiduNavi_575714.gif . . .] // startNode.pos.eType = BNCoordinate_BaiduMapSDK; //將起始點加入到節點數組中 [nodesArray addObject:startNode]; //***終點*** BNRoutePlanNode *endNode = [[BNRoutePlanNode alloc] init]; endNode.pos = [[BNPosition alloc] init]; endNode.pos.x = 114.077075; endNode.pos.y = 22.543634; endNode.pos.eType = BNCoordinate_BaiduMapSDK; [nodesArray addObject:endNode]; //***發起路徑規劃*** [BNCoreServices_RoutePlan startNaviRoutePlan:BNRoutePlanMode_Recommend naviNodes:nodesArray time:nil delegete:self userInfo:nil]; }
#pragma mark 算路成功回調-(void)routePlanDidFinished:(NSDictionary *)userInfo{ NSLog(@"算路成功"); //路徑規劃成功,開始導航 [BNCoreServices_UI showPage:BNaviUI_NormalNavi delegate:self extParams:nil]; }
-(void)routePlanDidFailedWithError:(NSError *)error andUserInfo:(NSDictionary *)userInfo{ NSLog(@"導航失敗"); }
1.官網百度導航SDK更新到了3.0.0,(3.0.0會有MBProgresshud等三方庫沖突問題,如果你的項目中使用了這些三方庫就會報錯),解決方法:使用百度導航3.02;
2.百度導航 TTS授權失敗,解決方法:去百度地圖官網申請成為開發者,再添加項目申請密鑰key;
3.百度導航SDK封裝的靜態庫單個文件就有200多M,git時最好添加到忽略文件裡。