IOS-高德地图路径绘制显示交通状况-Swift

本文基于:高德地图路径绘制进行了路径绘制的优化,添加了根据交通信息(是否拥堵)来显示路况,效果如图:
在这里插入图片描述

图标资源:

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
    }

相关推荐

  1. ISO Swift导航开发指南

    2024-01-21 10:32:03       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-21 10:32:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-21 10:32:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-21 10:32:03       87 阅读
  4. Python语言-面向对象

    2024-01-21 10:32:03       96 阅读

热门阅读

  1. Swift 分类继承

    2024-01-21 10:32:03       50 阅读
  2. springboot使用配置文件配置我们自己配置类的值

    2024-01-21 10:32:03       58 阅读
  3. ROS 2 Humble (Ubuntu Jammy)安装配置

    2024-01-21 10:32:03       67 阅读
  4. SpringSecurity(10)——Csrf防护

    2024-01-21 10:32:03       43 阅读
  5. React Hooks 基本使用

    2024-01-21 10:32:03       44 阅读
  6. 【Ubuntu】使用NFS共享挂载硬盘

    2024-01-21 10:32:03       56 阅读
  7. 数据库学习计划

    2024-01-21 10:32:03       60 阅读
  8. PHP服务报错500的排查过程

    2024-01-21 10:32:03       37 阅读
  9. HTTP 认证方式

    2024-01-21 10:32:03       58 阅读
  10. 小程序宿主环境-组件image

    2024-01-21 10:32:03       44 阅读
  11. pytorch基础 神经网络构建

    2024-01-21 10:32:03       44 阅读
  12. 数据库原理及数据库的优化

    2024-01-21 10:32:03       53 阅读
  13. PostgreSQL 100条命令

    2024-01-21 10:32:03       47 阅读
  14. 第三章 使用 SQL Search - 填充表

    2024-01-21 10:32:03       54 阅读