本文基于:高德地图路径绘制进行了路径绘制的优化,添加了根据交通信息(是否拥堵)来显示路况,效果如图:
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?.distance
let time=response.route.paths.first?.duration
let 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.status
mapView.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.title
polygonView?.lineWidth = 20.0
switch title {
case "缓行":
polygonView?.strokeImage=UIImage.init(resource: ImageResource.custtextureSlow)
break
case "拥堵":
polygonView?.strokeImage=UIImage.init(resource: ImageResource.custtextureBad)
break
case "严重拥堵":
polygonView?.strokeImage=UIImage.init(resource: ImageResource.custtextureSerious)
break
default://缓行、畅通
polygonView?.strokeImage=UIImage.init(resource: ImageResource.custtextureGreen)
}
return polygonView
}
return nil
}