package com.shoyoo.react.anavi.paths import android.content.Context import android.graphics.Color import com.amap.api.maps.model.* import com.amap.api.services.core.AMapException import com.amap.api.services.route.* import com.amap.api.services.route.RouteSearch.DriveRouteQuery import com.amap.api.services.route.RouteSearch.FromAndTo import com.facebook.react.bridge.Arguments import com.shoyoo.react.anavi.toLatLog import com.shoyoo.react.anavi.toLatLogPoint import com.shoyoo.react.anavi.R class AMapDrivePath(context: Context) : AMapPath(context) { init { pathColor = Color.parseColor("#537edc") routeSearch.setRouteSearchListener(this) } private var drivePath: DrivePath? = null set(value) { val oldValue = field field = value checkNeedDraw(oldValue, value) } var realTimeTraffic: Boolean = true override val defaultStartBitmapDescriptor: BitmapDescriptor get() = BitmapDescriptorFactory.fromResource(R.drawable.amap_start) override val defaultEndBitmapDescriptor: BitmapDescriptor get() = BitmapDescriptorFactory.fromResource(R.drawable.amap_end) override val defaultNodeBitmapDescriptor: BitmapDescriptor get() = BitmapDescriptorFactory.fromResource(R.drawable.amap_car) override val defaultThroughPointBitmapDescriptor: BitmapDescriptor get() = BitmapDescriptorFactory.fromResource(R.drawable.amap_through) override val bounds: LatLngBounds? get() { if (startPoint == null || endPoint == null) { return null } return LatLngBounds.builder().apply { include(startPoint!!.clone()) include(endPoint!!.clone()) throughPoints?.forEach { include(it?.toLatLog()) } }.build() } override fun calculate() { if (startPoint == null || endPoint == null) { return } val fromAndTo = FromAndTo(startPoint!!.toLatLogPoint(), endPoint!!.toLatLogPoint()) // 驾车路径规划 // 第一个参数表示路径规划的起点和终点, // 第二个参数表示驾车模式, // 第三个参数表示途经点, // 第四个参数表示避让区域, // 第五个参数表示避让道路 val query = DriveRouteQuery(fromAndTo, mode, wayPoints, avoidRegions, avoidRoad) // 异步路径规划驾车模式查询 routeSearch.calculateDriveRouteAsyn(query) // 分发事件 emit(id, "onSearchStart") } override fun onDriveRouteSearched(result: DriveRouteResult?, code: Int) { var eventCode = 0 if (code == AMapException.CODE_AMAP_SUCCESS) { if (result?.paths?.isEmpty() == false) { drivePath = result.paths[0] } else { eventCode = 1 } } else { eventCode = 2 } emit(id, "onSearchComplete", Arguments.createMap().apply { putInt("code", eventCode) }) } override fun draw() { if(!drawPath) { return } if (map == null) { return } if (drivePath == null) { return } if (startPoint == null || endPoint == null) { return } clear() val polylineOptions = PolylineOptions().apply { color(pathColor).width(pathWidth) } val tmCs = arrayListOf() drivePath?.steps?.forEach { step -> tmCs.addAll(step.tmCs) addDrivingStationMarkers(step, step.polyline[0].toLatLog()) polylineOptions.addAll(step.polyline.map { it.toLatLog() }) } addStartAndEndMarker() addThroughPointMarker() if (realTimeTraffic && tmCs.size > 0) { showTrafficPolyline(tmCs) } else { showPolyline(polylineOptions) } zoomToSpan() } private fun addDrivingStationMarkers(step: DriveStep, latLng: LatLng) { addNodeMarker(MarkerOptions().apply { position(latLng) title("方向:${step.action}道路:${step.road}") snippet(step.instruction) visible(nodeVisible) anchor(.5f, .5f) icon(nodeBitmapDescriptor) }) } private fun showPolyline(polylineOptions: PolylineOptions) { addPolyline(polylineOptions) } private fun showTrafficPolyline(tmCs: List) { if (map == null) { return } val trafficPolylineOptions = PolylineOptions().width(pathWidth) val colors = arrayListOf() trafficPolylineOptions.add(tmCs[0].polyline[0].toLatLog()) colors.add(pathColor) tmCs.forEach { tmc -> val color = getColorByStatus(tmc.status) tmc.polyline.forEach { polyline -> trafficPolylineOptions.add(polyline.toLatLog()) colors.add(color) } } colors.add(pathColor) trafficPolylineOptions.colorValues(colors) addPolyline(trafficPolylineOptions) } private fun getColorByStatus(status: String): Int { return when (status) { "畅通" -> normalColor "缓行" -> slowlyColor "拥堵" -> blockedColor "严重拥堵" -> severeBlockedColor else -> otherColor } } }