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.facebook.react.bridge.Arguments import com.shoyoo.react.anavi.toLatLog import com.shoyoo.react.anavi.toLatLogPoint import com.shoyoo.react.anavi.R import com.shoyoo.react.anavi.toMap class AMapWalkPath(context: Context) : AMapPath(context) { init { pathColor = Color.parseColor("#537edc") routeSearch.setRouteSearchListener(this) } private var walkPath: WalkPath? = null set(value) { val oldValue = field field = value checkNeedDraw(oldValue, value) } 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_man) override val defaultThroughPointBitmapDescriptor: BitmapDescriptor get() = BitmapDescriptorFactory.fromResource(R.drawable.amap_through) override fun calculate() { if (startPoint == null || endPoint == null) { return } val fromAndTo = RouteSearch.FromAndTo(startPoint!!.toLatLogPoint(), endPoint!!.toLatLogPoint()) val query = RouteSearch.WalkRouteQuery(fromAndTo) // 异步路径规划骑行模式查询 routeSearch.calculateWalkRouteAsyn(query) emit(id, "onSearchStart") } override fun draw() { if(!drawPath) { return } if (map == null) { return } if (walkPath == null) { return } if (startPoint == null || endPoint == null) { return } clear() val polylineOptions = PolylineOptions().apply { color(pathColor).width(pathWidth) } walkPath?.steps?.forEach { step -> addWalkStationMarkers(step, step.polyline[0].toLatLog()) polylineOptions.addAll(step.polyline.map { it.toLatLog() }) } addStartAndEndMarker() addThroughPointMarker() addPolyline(polylineOptions) zoomToSpan() } override fun onWalkRouteSearched(result: WalkRouteResult?, code: Int) { var eventCode = 0 if (code == AMapException.CODE_AMAP_SUCCESS) { if (result?.paths?.isEmpty() == false) { walkPath = result.paths[0] } else { eventCode = 1 } } else { eventCode = 2 } emit(id, "onSearchComplete", Arguments.createMap().apply { putInt("code", eventCode) putMap("path", walkPath?.toMap()) }) } private fun addWalkStationMarkers(walkStep: WalkStep, position: LatLng) { addNodeMarker(MarkerOptions() .position(position) .title("方向:${walkStep.action} 道路:${walkStep.road}") .snippet(walkStep.instruction).visible(nodeMarkerVisible) .anchor(0.5f, 0.5f).icon(nodeBitmapDescriptor)) } }