前言

明天分享腾讯位置服务地图SDK检索性能的利用,应用公交路线布局性能实现Demo,临时还没有做同一路线不同公交线路切换性能(后续欠缺此Demo)。

应用场景

公交路线布局

筹备

腾讯位置服务iOS地图SDK

公交路线布局检索后果的数据阐明

1、检索后果:QMSBusingRouteSearchResult中的NSArray *routes属性蕴含了检索到的后果,每个后果都是一个独立的公交出行计划QMSBusingRoutePlan。

2、公交出行计划:QMSBusingRoutePlan,示意从终点到起点的残缺出行布局

属性阐明
CGFloat distance计划的总间隔
CGFloat duration计划的预估工夫
NSString *bounds计划的西南、东北坐标,用于调整地图视角显示路线
NSArray <QMSBusingSegmentRoutePlan *> *steps总路线的分段信息(蕴含步行、公交,其中公交可能蕴含多个计划)

3、出行计划的分段路线布局

属性阐明
NSString *mode"WALKING":步行
"TRANSIT":公交
标记该分段的出行形式
CGFloat distance分段的间隔
CGFloat duration分段的预估工夫
CGFloat price如果是公交或者地铁,须要破费的金额,元
CNSString *direction方向形容,如果为步行,表白为向哪个方向走
NSArray *polyline分段路径坐标点(这里必须说一下,腾讯地图SDK应用的是CLLocationCoordinate2D的encode类型,因而还须要decode能力应用)
NSArray *lines如果是公交,并且有多个线路能够乘坐的话,该数组就会蕴含多个路线:QMSBusingRouteTransitLine

4、公交线路布局:QMSBusingRouteTransitLine,到这里才是最麻烦的中央,因为是一个线路的不同计划

属性阐明
NSString *vehicle交通工具:公交、地铁
NSString *id_应该是个标记,没发现作用
CGFloat distance间隔
NSTimeInterval duration预计乘坐工夫
NSString *title车名:如333路、软件园通勤车等
NSArray *polyline路径坐标点
NSInteger station_count路径站的数目
NSArray<QMSBusStation *> *stations路径站的站名
QMSStationEntrance *destination目的地的地址,也就是属于哪个街道
QMSBusStation *geton上车车站
QMSBusStation *getoff下车车站

公交路线布局的具体阐明

1、公交路线布局的终点和起点坐标的检索(不反对地名检索)

1)SDK检索参数并不反对地名检索,只有坐标检索,因而要应用检索性能就必须先通过POI检索性能来获取终点和起点的坐标地位:

- (IBAction)searchButtonClicked:(UIButton *)sender {    _startPoiData = nil;    _endPoiData = nil;        // 终点:腾讯北京总部    _startPoiOption = [[QMSPoiSearchOption alloc] init];    _startPoiOption.keyword = _startPointTextField.text;    [_startPoiOption setBoundaryByRegionWithCityName:@"北京" autoExtend:NO];    [self.mapSearcher searchWithPoiSearchOption:_startPoiOption];        // 起点:西二旗地忒站    _endPoiOption = [[QMSPoiSearchOption alloc] init];    _endPoiOption.keyword = _endPointTextField.text;    [_endPoiOption setBoundaryByRegionWithCityName:@"北京" autoExtend:NO];    [self.mapSearcher searchWithPoiSearchOption:_endPoiOption];}- (void)searchWithPoiSearchOption:(QMSPoiSearchOption *)poiSearchOption didReceiveResult:(QMSPoiSearchResult *)poiSearchResult {    if (poiSearchOption == _startPoiOption) {        _startPoiData = poiSearchResult.dataArray.firstObject;    } else {        _endPoiData = poiSearchResult.dataArray.firstObject;    }            // 增加终点起点前,先打消之前的大头针    [self.mapView removeAnnotations:self.mapView.annotations];        // 设置终点和起点    QPointAnnotation *startPointAnnotation = [[QPointAnnotation alloc] init];    startPointAnnotation.coordinate = _startPoiData.location;    [self.mapView addAnnotation:startPointAnnotation];        QPointAnnotation *endPointAnnotation = [[QPointAnnotation alloc] init];    endPointAnnotation.coordinate = _endPoiData.location;    [self.mapView addAnnotation:endPointAnnotation];        // 发动公交路线布局检索    [self searchBusRoute];}

2)通过终点和起点POI数据来发动公交路线布局检索

- (void)searchBusRoute {    if (_startPoiData != nil && _endPoiData != nil) {        QMSBusingRouteSearchOption *option = [[QMSBusingRouteSearchOption alloc] init];        [option setFromCoordinate:_startPoiData.location];        [option setToCoordinate:_endPoiData.location];                [self.mapSearcher searchWithBusingRouteSearchOption:option];    }}

2、显示所有路线的根本信息

1)、获取计划的数量:planCount

NSInteger planCount = busingRouteSearchResult.routes.count;

2)、创立路线计划模型数组,在这里我在保留了路线的共事,提出了几样数据:

// 路线计划@property (nonatomic, strong) QMSBusingRoutePlan *routePlan;// 路线预估工夫@property (nonatomic, assign) NSInteger duration;// 路线的步行间隔@property (nonatomic, assign) CGFloat distance;// 路径的公交车站数@property (nonatomic, assign) NSInteger stationCount;// 上车站名称@property (nonatomic, strong) NSString *startStationName;

3)、获取路线的步行间隔:须要遍历计划中的所有分段信息,判断是否为步行,而后累加distance:

// 遍历计划中的步骤for (int i = 0; i < plan.steps.count; i++) {    QMSBusingSegmentRoutePlan *segmentRoutePlan = plan.steps[i];        // 判断步骤为步行,累计间隔    if ([self routeIsWalkingPlan:segmentRoutePlan]) {        walkingDistance += segmentRoutePlan.distance;    }}

4)、获取路径的公交站总数和上车站名称:因为同一分段可能有多个公交出行计划,在此只演示了默认第一条计划:

// 判断步骤为公交if ([self routeIsTransitPlan:segmentRoutePlan]) {    // 遍历通过的公交    for (int j = 0; j < segmentRoutePlan.lines.count; j++) {        QMSBusingRouteTransitLine *line = segmentRoutePlan.lines[j];        // 公交站数        stationCount += line.station_count;                // 第一站名称        if (j == 0) {            QMSBusStation *station = line.stations.firstObject;            startStationName = station.title;        }    }}

5)、通过TableView来展现所有的计划:

3、抉择计划,展现路线图

1)、先获取该计划总共的分段数:

NSInteger stepCount = routePlan.steps.count;

2)、通过判断分段计划的类型,来辨别虚线和蚯蚓线:

if ([self routeIsWalkingPlan:plan]) {    // 步行虚线    CLLocationCoordinate2D coords[plan.polyline.count];    for (int i = 0; i < plan.polyline.count; i++) {        NSValue *value = plan.polyline[i];        CLLocationCoordinate2D coord = [value coordinateValue];        coords[i] = coord;    }    QPolyline *walkingPolyline = [[QPolyline alloc] initWithCoordinates:coords count:plan.polyline.count];    // QPolyline中的userData属性能够用来增加自定义的内容去判断数据    walkingPolyline.userData = @"WALKING";    [self.mapView addOverlay:walkingPolyline];    } else {    // 驾车蚯蚓线    QMSBusingRouteTransitLine *line = plan.lines[0];    CLLocationCoordinate2D coords[line.polyline.count];    for (int i = 0; i < line.polyline.count; i++) {        NSValue *value = line.polyline[i];        CLLocationCoordinate2D coord = [value coordinateValue];        coords[i] = coord;    }    QPolyline *busPolyline = [[QPolyline alloc] initWithCoordinates:coords count:line.polyline.count];    busPolyline.userData = @"TRANSIT";    [self.mapView addOverlay:busPolyline];}

3)、最初,实现代理办法去绘制线路:

- (QOverlayView *)mapView:(QMapView *)mapView viewForOverlay:(id<QOverlay>)overlay {    if ([overlay isKindOfClass:[QPolyline class]]) {        QPolyline *polyline = (QPolyline *)overlay;        NSString *userData = (NSString *)polyline.userData;                if ([userData isEqualToString:@"WALKING"]) {            // 步行虚线            QPolylineView *polylineView = [[QPolylineView alloc] initWithPolyline:polyline];            polylineView.lineWidth = 8;            polylineView.lineDashPattern = @[@15, @10];            polylineView.strokeColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.5];                        return polylineView;        } else {            // 驾车蚯蚓线            QTexturePolylineView *polylineView = [[QTexturePolylineView alloc] initWithPolyline:polyline];            polylineView.drawType = QTextureLineDrawType_ColorLine;            // 路线箭头            polylineView.drawSymbol = YES;            polylineView.lineWidth = 8;            polylineView.strokeColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.8];                        return polylineView;        }    }        return nil;}

4、补充:当增加结束之后,让地图的视线主动适配路线

1)、路线计划中的NSString *bounds属性标记了东北、西南两个方向的坐标,然而这个两个坐标组合成了一个字符串,还须要咱们本人去解析:

- (QCoordinateBounds)boundsFromString:(NSString *)boundsString {    NSArray *array = [boundsString componentsSeparatedByString:@","];    NSString *southWestLatString = array[0];    NSString *southWestLongString = array[1];    NSString *northEastLatString = array[2];    NSString *northEastLongString = array[3];    CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(northEastLatString.floatValue, northEastLongString.floatValue);    CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(southWestLatString.floatValue, southWestLongString.floatValue);        QCoordinateBounds bounds;    bounds.northEast = northEast;    bounds.southWest = southWest;        return bounds;}

2)、最初调用地图接口,调整地图显示范畴:

[self.mapView setVisibleMapRect:QMapRectForCoordinateBounds(bounds) edgePadding:UIEdgeInsetsMake(10, 10, 10, 10) animated:YES];

5、最终显示成果:

作者:面糊

链接:https://www.jianshu.com/p/12b...

起源:简书

著作权归作者所有。商业转载请分割作者取得受权,非商业转载请注明出处。