一、设置起始坐标点和路线监听事件
/**
* 绘制路线规划
*/
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))