標籤:ios 百度地圖 連線
最近在做一個小的demo,試一下軌跡記錄。
記錄軌跡需要不停的擷取位置記錄到資料庫。
在畫折現的時候會在道路拐角處直接連線,不會與道路貼合,在這說一下我的解決方案。
我調用了百度地圖的路徑規劃api。這樣就能實現路徑貼合功能了。
在此附上dome:點擊開啟連結
強調:這個dome是真機上運行,在模擬器上會報錯。下載的時候注意一下。
由於各種原因圖片是用手機拍的。
上張圖看看:
核心代碼:
調用駕車線路規劃,規划出從這點到那點的線路然後畫出路線,產生的線路在下面函數中獲得。
- (void)onGetDrivingRouteResult:(BMKRouteSearch *)searcher result:(BMKDrivingRouteResult *)result errorCode:(BMKSearchErrorCode)error
上面這是代理方法。
BMKPlanNode *startNode = [[BMKPlanNode alloc]init]; CLLocationCoordinate2D startCoordinate; startCoordinate.latitude =36.727558; startCoordinate.longitude =119.185956; startNode.pt = startCoordinate; BMKPlanNode *endNode = [[BMKPlanNode alloc]init]; CLLocationCoordinate2D endCoordnate; endCoordnate.latitude =36.827558; endCoordnate.longitude =119.385956; endNode.pt = endCoordnate; BMKDrivingRoutePlanOption * drivingRoutePlanOption = [[BMKDrivingRoutePlanOption alloc]init]; drivingRoutePlanOption.from = startNode; drivingRoutePlanOption.to = endNode; if ([_searcher drivingSearch:drivingRoutePlanOption]) { NSLog(@"路線尋找成功"); }
- (void)onGetDrivingRouteResult:(BMKRouteSearch *)searcher result:(BMKDrivingRouteResult *)result errorCode:(BMKSearchErrorCode)error{ BMKDrivingRouteLine *plan = (BMKDrivingRouteLine *)[result.routes objectAtIndex:0]; int size = (int)[plan.steps count]; int pointCount = 0; for (int i = 0; i< size; i++) { BMKDrivingStep *step = [plan.steps objectAtIndex:i]; pointCount += step.pointsCount; } BMKMapPoint *points = new BMKMapPoint[pointCount]; int k = 0; for (int i = 0; i< size; i++) { BMKDrivingStep *step = [plan.steps objectAtIndex:i]; for (int j= 0; j<step.pointsCount; j++) { points[k].x = step.points[j].x; points[k].y = step.points[j].y; k++; } } NSLog(@"點的個數:(%d)",k); BMKPolyline *polyLine = [BMKPolyline polylineWithPoints:points count:pointCount]; [_mapView addOverlay:polyLine];}在上面方法中我們從返回的線路中 擷取該線路的路段,再從路段中擷取到路段中的點,最後對這些點進行畫線。
iOS 百度地圖 軌跡記錄 道路貼合