全部服务产品
开发者频道
定价
登录
路名绘制
下载开发文档
一、BMKTextPathMarker简介

Since V6.6.3地图SDK支持BMKTextPathMarker路名绘制;注意BMKTextPathMarker与普通Marker无关,是个单独的overlay

二、BMKTextPathMarker使用
1. 创建BMKMapView,并设置delegate
BMKMapView *mapView = [[BMKMapView alloc]initWithFrame:self.view.bounds];
mapView.delegate = self;
[self.view addSubView:mapView];
2. 创建BMKTextPathMarker并添加到地图上

使用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 ++) {
//将每条路段所经过的地理坐标点赋值给points
points[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];
// 释放points
delete[] points;
points = nil;
// 添加路线
[_mapView addOverlay:polyline];
// 添加路名
if (routeNames && routeNames.count) {
[_mapView addOverlays:routeNames];
}
}
}
3. 实现代理方法
/// 根据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;
}

运行效果如下:

IMG_1884.png

上一篇

绘制线

下一篇

绘制弧线和面

本篇文章对您是否有帮助?