本文基于:高德地图路径绘制进行了路径绘制的优化,添加了根据交通信息(是否拥堵)来显示路况,效果如图:
图标资源:
custtexture_bad
custtexture_green
custtexture_slow
custtexture_serious
设置覆盖物
//路径搜索结果func onRouteSearchDone(_ request: AMapRouteSearchBaseRequest!, response: AMapRouteSearchResponse!) {// 取出第一种路线方案let stringWithOptional = response.route.paths.first?.polyline!let distance=response.route.paths.first?.distancelet time=response.route.paths.first?.durationlet steps=response.route.paths.first?.steps//遍历导航路段 AMapStep 数组for step in steps! {//遍历路况信息数组for tmcs in step.tmcs{let result = convertToArray(step.polyline)if var temp = result {let polyline = MAPolyline.init(coordinates: &temp, count: UInt(temp.count))//设置路况状态描述为标题:0 未知,1 畅通,2 缓行,3 拥堵,4 严重拥堵polyline?.title=tmcs.statusmapView.add(polyline)overlays.append(polyline!)}}}//添加动画效果mapView.showOverlays(overlays, edgePadding: UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20), animated: true)print("距离:\(distance!)米,预计耗时:\(time!)秒")}
设置图标资源
//路径绘制代理func mapView(_ mapView: MAMapView!, rendererFor overlay: MAOverlay!) -> MAOverlayRenderer! {if overlay is MAPolyline {let polygonView = MAPolylineRenderer.init(polyline: (overlay as! MAPolyline))// 参数设置let title=overlay.titlepolygonView?.lineWidth = 20.0switch title {case "缓行":polygonView?.strokeImage=UIImage.init(resource: ImageResource.custtextureSlow)breakcase "拥堵":polygonView?.strokeImage=UIImage.init(resource: ImageResource.custtextureBad)breakcase "严重拥堵":polygonView?.strokeImage=UIImage.init(resource: ImageResource.custtextureSerious)breakdefault://缓行、畅通polygonView?.strokeImage=UIImage.init(resource: ImageResource.custtextureGreen)}return polygonView}return nil}