iOS 地图线路动态可视化显示
#pragma mark - 驾车线路检索 - (void)onGetDrivingRouteResult:(BMKRouteSearch*)searcher result:(BMKDrivingRouteResult*)result errorCode:(BMKSearchErrorCode)error { if (error == BMK_SEARCH_NO_ERROR) { //在此处理正常结果 NSArray* array = [NSArray arrayWithArray:_mapView.annotations]; [_mapView removeAnnotations:array]; array = [NSArray arrayWithArray:_mapView.overlays]; [_mapView removeOverlays:array]; if (error == BMK_SEARCH_NO_ERROR) { BMKDrivingRouteLine* plan = (BMKDrivingRouteLine*)[result.routes objectAtIndex:0]; // 计算路线方案中的路段数目 int size = [plan.steps count]; int planPointCounts = 0; for (int i = 0; i < size; i++) { BMKDrivingStep* transitStep = [plan.steps objectAtIndex:i]; if(i==0){ RouteAnnotation* item = [[RouteAnnotation alloc]init]; item.coordinate = plan.starting.location; item.title = @"起点"; item.type = 0; [_mapView addAnnotation:item]; // 添加起点标注 }else if(i==size-1){ RouteAnnotation* item = [[RouteAnnotation alloc]init]; item.coordinate = plan.terminal.location; item.title = @"终点"; item.type = 1; [_mapView addAnnotation:item]; // 添加起点标注 } //添加annotation节点 RouteAnnotation* item = [[RouteAnnotation alloc]init]; item.coordinate = transitStep.entrace.location; item.title = transitStep.entraceInstruction; item.degree = transitStep.direction * 30; item.type = 4; [_mapView addAnnotation:item]; //轨迹点总数累计 planPointCounts += transitStep.pointsCount; //----------------------------------------------------------- if (i==0) { //以第一个坐标点做初始值 minLat = plan.starting.location.latitude; maxLat = plan.starting.location.latitude; minLon = plan.starting.location.longitude; maxLon = plan.starting.location.longitude; }else{ //对比筛选出最小纬度,最大纬度;最小经度,最大经度 minLat = MIN(minLat, transitStep.entrace.location.latitude); maxLat = MAX(maxLat, transitStep.entrace.location.latitude); minLon = MIN(minLon, transitStep.entrace.location.longitude); maxLon = MAX(maxLon, transitStep.entrace.location.longitude); } //----------------------------------------------------------- } [self setVisibleRegin]; // 添加途经点 if (plan.wayPoints) { for (BMKPlanNode* tempNode in plan.wayPoints) { RouteAnnotation* item = [[RouteAnnotation alloc]init]; item = [[RouteAnnotation alloc]init]; item.coordinate = tempNode.pt; item.type = 5; item.title = tempNode.name; [_mapView addAnnotation:item]; } } //轨迹点 BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts]; int i = 0; for (int j = 0; j < size; j++) { BMKDrivingStep* transitStep = [plan.steps objectAtIndex:j]; int k=0; for(k=0;k<transitStep.pointsCount;k++) { temppoints[i].x = transitStep.points[k].x; temppoints[i].y = transitStep.points[k].y; i++; } } // 通过points构建BMKPolyline BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts]; [_mapView addOverlay:polyLine]; // 添加路线overlay delete []temppoints; } } else { NSLog(@"抱歉,未找到结果"); } }
- (void)setVisibleRegin { //计算中心点 CLLocationCoordinate2D centCoor; centCoor.latitude = (CLLocationDegrees)((maxLat+minLat) * 0.5f); centCoor.longitude = (CLLocationDegrees)((maxLon+minLon) * 0.5f); BMKCoordinateSpan span; //计算地理位置的跨度 span.latitudeDelta = maxLat - minLat; span.longitudeDelta = maxLon - minLon; //得出数据的坐标区域 BMKCoordinateRegion region = BMKCoordinateRegionMake(centCoor, span); //百度地图的坐标范围转换成相对视图的位置 CGRect fitRect = [_mapView convertRegion:region toRectToView:_mapView]; //将地图视图的位置转换成地图的位置, BMKMapRect fitMapRect = [_mapView convertRect:fitRect toMapRectFromView:_mapView]; //设置地图可视范围为数据所在的地图位置 [_mapView setVisibleMapRect:fitMapRect animated:YES]; }欢迎大家批评指正!!!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。