preface

Continue the previous Demo, this time add the same route of multiple different bus lines display, default display 0. And click on the bus route to switch the selected route.

Previous reference: Tencent map SDK bus route planning Demo

Usage scenarios

Bus Route Planning

To prepare

Tencent location service iOS map SDK

Line switch

1. Create a subclass of QPolyline to distinguish the walking part from the public part.

@interface RoutePlanWalkingPolyline : QPolyline

@end
Copy the code
@interface RoutePlanBusingPolyline: QPolyline @property (nonatomic, assign) BOOL isSelected; @endCopy the code

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

/* 1. Select the first bus route by default. 2. For (int I = 0; 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]; }Copy the code

3. When adding the broken line view, add an additional step to determine whether the current bus model has been selected.

RoutePlanBusingPolyline *busingPolyline = (RoutePlanBusingPolyline *)overlay; / / route if arrow (busingPolyline. An 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; }Copy the code

4. Finally, listen for the Overlay click method and determine whether the Overlay callback is a data model for the bus route. Then check the model and uncheck the other models.

- (void)mapView:(QMapView *)mapView didTapOverlay:(id<QOverlay>)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) {/ / selected: solid color busingPolyline. An isSelected = YES; StrokeColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5]; polylineView.zIndex = 1; polylineView.drawSymbol = YES; } else {/ / not selected: virtual color busingPolyline. An isSelected = NO. StrokeColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.5]; polylineView.zIndex = 0; polylineView.drawSymbol = NO; } } } } }Copy the code

figure

Akik: Batter

Link: www.jianshu.com/p/74b6b7b50…

Source: Jane Book

Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.