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.RidePath import com.amap.api.services.route.RideRouteResult import com.amap.api.services.route.RideStep import com.amap.api.services.route.RouteSearch.FromAndTo import com.amap.api.services.route.RouteSearch.RideRouteQuery 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 AMapRidePath(context: Context): AMapPath(context) { init { pathColor = Color.parseColor("#537edc") routeSearch.setRouteSearchListener(this) } private var ridePath: RidePath? = 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_ride) override val defaultThroughPointBitmapDescriptor: BitmapDescriptor get() = BitmapDescriptorFactory.fromResource(R.drawable.amap_through) override fun calculate() { if (startPoint == null || endPoint == null) { return } val fromAndTo = FromAndTo(startPoint!!.toLatLogPoint(), endPoint!!.toLatLogPoint()) val query = RideRouteQuery(fromAndTo) // 异步路径规划骑行模式查询 routeSearch.calculateRideRouteAsyn(query) emit(id, "onSearchStart") } override fun draw() { if(!drawPath) { return } if (map == null) { return } if (ridePath == null) { return } if (startPoint == null || endPoint == null) { return } clear() val polylineOptions = PolylineOptions().apply { color(pathColor).width(pathWidth) } ridePath?.steps?.forEach { step -> addRideStationMarkers(step, step.polyline[0].toLatLog()) polylineOptions.addAll(step.polyline.map { it.toLatLog() }) } addStartAndEndMarker() addThroughPointMarker() addPolyline(polylineOptions) zoomToSpan() } override fun onRideRouteSearched(result: RideRouteResult?, code: Int) { var eventCode = 0 if (code == AMapException.CODE_AMAP_SUCCESS) { if (result?.paths?.isEmpty() == false) { ridePath = result.paths[0] } else { eventCode = 1 } } else { eventCode = 2 } emit(id, "onSearchComplete", Arguments.createMap().apply { putInt("code", eventCode) putMap("path", ridePath?.toMap()) }) } private fun addRideStationMarkers(rideStep: RideStep, position: LatLng) { addNodeMarker(MarkerOptions() .visible(nodeMarkerVisible) .position(position) .title("方向:${rideStep.action} 道路:${rideStep.road}") .snippet(rideStep.instruction) .anchor(0.5f, 0.5f) .icon(nodeBitmapDescriptor)) } }