乐趣区

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

前言

持续上一次的 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;
                }
            }
        }
    }
}

示例图

作者:面糊

链接:https://www.jianshu.com/p/74b…

起源:简书

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

退出移动版