Tencent location service map SDK bus route planning application example 2

Preface

Continuing the last demo, this time we added multiple different bus line displays of the same route, and the default display is 0. And by clicking the bus route to switch the selected route.

The previous reference: Tencent location service map SDK bus route planning application example

scenes to be used

Bus route planning

ready

Tencent Location Service iOS Map SDK

Route switching

1. Create a sub-category of QPolyline, which is used to distinguish the pedestrian part from the bus part.

@interface RoutePlanWalkingPolyline : QPolyline

@end
@interface RoutePlanBusingPolyline : QPolyline

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

@end

2. When creating route Mode, create different models by judging the type of route, and select the 0th route by default.

/*
 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. When adding a polyline view, an additional step is added to determine whether the current bus model has been selected.

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. Finally, monitor the click method of the Overlay and determine whether the callback is the data model of the bus route, and then select the model and uncheck other models.

- (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;
                }
            }
        }
    }
}

sample graph

Author: batter

Link: https://www.jianshu.com/p/74b6b7b50352

Source: Brief Book

The copyright belongs to the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/weixin_45628602/article/details/111325947