高德地图 路线规划笔记

一、设置起始坐标点和路线监听事件

/**
     * 绘制路线规划
     */
    private fun drawDriverLines(index: Int) {
        // CoordinateUtils坐标转换工具类 使用 blankj 的 AndroidUtilCode
        // 百度坐标转高德坐标系 --- 终止点
        val gcArray = CoordinateUtils.bd09ToGcj02(
            lng = datas!![index].longitude.toDouble(),
            lat = datas!![index].latitude.toDouble()
        )
        // 起始和终止地点坐标,并进行查询返回(异步)
        val fromTo: RouteSearch.FromAndTo = RouteSearch.FromAndTo(
            LatLonPoint(GlobalVariable.LATITUDE, GlobalVariable.LONGITUDE),
            LatLonPoint(gcArray[1], gcArray[0])
        )
        val query: RouteSearch.DriveRouteQuery =
            RouteSearch.DriveRouteQuery(fromTo, 2, null, null, "")
        val routeSearch = RouteSearch(context)
        // 异步查询
        routeSearch.calculateDriveRouteAsyn(query)
        // 设置路线查询监听
        routeSearch.setRouteSearchListener(this)
    }

二、监听事件添加路线规划


    // ------------  以下是监听函数 --------------

    /**
     * 推荐路程
     */
    override fun onDriveRouteSearched(driveRoutePlanResult: DriveRouteResult?, p1: Int) {
        val pathList = driveRoutePlanResult!!.paths
        LogUtils.d("行程路线 数量 --->> ${pathList.size}")
        val driverPath = ArrayList<LatLng>()
        val colorList = ArrayList<Int>()
        for (dp in pathList) {
            val stepList = dp.steps
            for (ds in stepList) {
                /* val  points = ds.polyline
                 for (llp in points) {
                     driverPath.add( LatLng(llp.latitude, llp.longitude))
                 }*/
                val tmcs = ds.tmCs
                for (tmc in tmcs) {
                    val status = tmc.status
                    LogUtils.d("路况状态 --->> $status")
                    when (status) {
                        "畅通" -> {
                            colorList.add(Color.argb(255, 122, 142, 239))
                        }
                        "缓行" -> {
                            colorList.add(Color.argb(255, 220, 179, 48))
                        }
                        "拥堵" -> {
                            colorList.add(Color.argb(255, 240, 110, 100))
                        }
                        "严重拥堵" -> {
                            colorList.add(Color.argb(255, 250, 110, 100))
                        }
                        "未知" -> {
                            colorList.add(Color.argb(255, 220, 179, 48))
                        }
                    }
                    val polyList = tmc.polyline
                    for (poly in polyList) {
                        driverPath.add(LatLng(poly.latitude, poly.longitude))
                    }
                }
            }
        }

        if (polyLine != null) {
            polyLine!!.remove()
        }
        polyLine = aMap!!.addPolyline(PolylineOptions().colorValues(colorList).addAll(driverPath))
        setMarker(datas!!)
    }

    override fun onBusRouteSearched(p0: BusRouteResult?, p1: Int) {
    }

    override fun onRideRouteSearched(p0: RideRouteResult?, p1: Int) {
    }

    override fun onWalkRouteSearched(p0: WalkRouteResult?, p1: Int) {
    }

三、每次点击marker需要清空之前的路线

   if (polyLine != null) {
         polyLine!!.remove()
    }
        polyLine = aMap!!.addPolyline(PolylineOptions().colorValues(colorList).addAll(driverPath))
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值