Since V6.6.3地图SDK支持BMKTextPathMarker路名绘制;注意BMKTextPathMarker与普通Marker无关,是个单独的overlay
BMKMapView *mapView = [[BMKMapView alloc]initWithFrame:self.view.bounds];mapView.delegate = self;[self.view addSubView:mapView];
使用BMKTextPathMarker创建一个带有text文本的Overlay,调用BMKMapView的addOverlay或addOverlays方法添加BMKTextPathMarker到地图上。
以下是配合驾车路线规划接口数据,绘制的路线及路名:
- (void)onGetDrivingRouteResult:(BMKRouteSearch*)searcher result:(BMKDrivingRouteResult*)result errorCode:(BMKSearchErrorCode)error {NSLog(@"onGetDrivingRouteResult");// [_mapView removeOverlays:_mapView.overlays];// [_mapView removeAnnotations:_mapView.annotations];if (error == BMK_SEARCH_NO_ERROR) {NSMutableArray *routeNames = [NSMutableArray array];//获取所有驾车路线中第一条路线BMKDrivingRouteLine *routeline = (BMKDrivingRouteLine *)result.routes.firstObject;NSMutableArray<NSNumber *> *textIndexs = [NSMutableArray array];/// 路段的路况信息,成员为NSNumber。0:无数据;1:畅通;2:缓慢;3:拥堵// @property (nonatomic, copy) NSArray <NSNumber *> *traffics;//+polylineWithPoints: count:指定的直角坐标点数组BMKMapPoint *points = new BMKMapPoint[routeline.totalPointsCount];__block NSUInteger j = 0;//遍历驾车路线中的所有路段[routeline.steps enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {//获取驾车路线中的每条路段BMKDrivingStep *step = routeline.steps[idx];//路名相关BMKTextPathMarker *routeName = [BMKTextPathMarker textPathMarkerWithPoints:step.points count:step.pointsCount];routeName.text = step.roadName;BMKTextStyle *style = [[BMKTextStyle alloc] init];style.textColor = [UIColor blackColor];style.fontSize = 25;style.borderColor = [UIColor whiteColor];style.borderWidth = 1;routeName.style = style;[routeNames addObject:routeName];//遍历每条路段所经过的地理坐标集合点for (NSUInteger i = 0; i < step.pointsCount; i ++) {//将每条路段所经过的地理坐标点赋值给pointspoints[j].x = step.points[i].x;points[j].y = step.points[i].y;j ++;}for (int k = 0; k < step.traffics.count; k ++) {[textIndexs addObject:step.traffics[k]];}}];BMKMultiPolyline *polyline = [BMKMultiPolyline multiPolylineWithPoints:points count:routeline.totalPointsCount drawIndexs:textIndexs];// 释放pointsdelete[] points;points = nil;// 添加路线[_mapView addOverlay:polyline];// 添加路名if (routeNames && routeNames.count) {[_mapView addOverlays:routeNames];}}}
/// 根据overlay生成对应的View/// @param mapView 地图View/// @param overlay 指定的overlay/// @return 生成的覆盖物View- (__kindof BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay {if ([overlay isKindOfClass:[BMKMultiPolyline class]]) {BMKMultiTexturePolylineView *multiTexturePolylineView = [[BMKMultiTexturePolylineView alloc] initWithMultiPolyline:overlay];multiTexturePolylineView.lineWidth = 8.0;multiTexturePolylineView.textureImages = @[[UIImage imageNamed:@"traffic_texture_congestion"],[UIImage imageNamed:@"traffic_texture_slow"],[UIImage imageNamed:@"traffic_texture_smooth"],[UIImage imageNamed:@"traffic_texture_unknown"]];return multiTexturePolylineView;}else if ([overlay isKindOfClass:[BMKTextPathMarker class]]) {BMKTextPathMarkerView *textPathMarker = [[BMKTextPathMarkerView alloc] initWithMarker:overlay];return textPathMarker;}return nil;}
运行效果如下:
上一篇
下一篇
本篇文章对您是否有帮助?