前言

继续上一次的Demo,本次添加了同一路线的多条不同的公交线路展示,默认显示第0条。并且通过点击公交路线来切换选中的路线。

上篇参考:腾讯位置服务地图SDK公交路线规划应用示例

使用场景

公交路线规划

准备

腾讯位置服务iOS地图SDK

路线切换

1、创建QPolyline的子类,用于区分步行部分和公交部分。

@interface RoutePlanWalkingPolyline : QPolyline

@end
@interface RoutePlanBusingPolyline : QPolyline

// 判断当前路线是否已经被选中
@property (nonatomic, assign) BOOL isSelected;

@end

2、在创建路线Mode时,通过判断路线的种类来创建不同的模型,并且默认选中第0条路线。

/*
 1. 默认选择第一个公交车线路
 2. 当前公交车线路 alpha = 1,有箭头
 3. 备用公交车线路 alpha = 0.5, 无箭头
 4. 点击备用公交车线路可以切换
 */
// 驾车蚯蚓线
// 遍历所有驾车路线
for (int i = 0; i < plan.lines.count; i++) {
    QMSBusingRouteTransitLine *line = plan.lines[i];
    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;
    }

    RoutePlanBusingPolyline *busPolyline = [[RoutePlanBusingPolyline alloc] initWithCoordinates:coords count:line.polyline.count];
    busPolyline.isSelected = i==0 ? YES : NO;
    [self.mapView addOverlay:busPolyline];
    [self.selectRouteOverlayArray addObject:busPolyline];
}

3、在添加折线视图时,额外添加判断当前的公交模型是否已经被选中的步骤。

RoutePlanBusingPolyline *busingPolyline = (RoutePlanBusingPolyline *)overlay;
// 路线箭头
if (busingPolyline.isSelected) {

    polylineView.strokeColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5];
    polylineView.drawSymbol = YES;
    polylineView.zIndex = 1;
} else {
    polylineView.strokeColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.5];
    polylineView.drawSymbol = NO;
    polylineView.zIndex = 0;
}

4、最后,监听Overlay的点击方法,并判断回调的Overlay是否为公交路线的数据模型,然后选中该模型,取消选中其他的模型。

- (void)mapView:(QMapView *)mapView didTapOverlay:(id<QOverlay>)overlay {
    // 判断是否是路线
    if ([overlay isKindOfClass:[RoutePlanBusingPolyline class]]) {

        for (QPolyline *polyline in self.selectRouteOverlayArray) {
            if ([polyline isKindOfClass:[RoutePlanBusingPolyline class]]) {

                RoutePlanBusingPolyline *busingPolyline = (RoutePlanBusingPolyline *)polyline;

                QTexturePolylineView *polylineView = (QTexturePolylineView *)[self.mapView viewForOverlay:busingPolyline];

                if (busingPolyline == overlay) {
                    // 选中:实心颜色
                    busingPolyline.isSelected = YES;
                    polylineView.strokeColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5];
                    polylineView.zIndex = 1;
                    polylineView.drawSymbol = YES;
                } else {
                    // 未选中:虚色
                    busingPolyline.isSelected = NO;
                    polylineView.strokeColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.5];
                    polylineView.zIndex = 0;
                    polylineView.drawSymbol = NO;
                }
            }
        }
    }
}

示例图

腾讯位置服务地图SDK公交路线规划应用示例2-LMLPHP

腾讯位置服务地图SDK公交路线规划应用示例2-LMLPHP

05-16 05:29