DrivingRoute.kt
7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package cn.feewee.amap3d.map_view.route
import android.content.Context
import android.graphics.Color
import android.widget.Toast
import cn.feewee.amap3d.*
import cn.feewee.amap3d.R
import com.amap.api.maps.AMap
import com.amap.api.maps.model.*
import com.amap.api.services.core.AMapException
import com.amap.api.services.route.*
import com.facebook.react.bridge.ReadableArray
class DrivingRoute(context: Context) : RouteOverlay(context) {
private var drivePath: DrivePathV2? = null
private var throughPointList: List<LatLng> = emptyList()
private var isColorfulline = true
private var throughPointMarkerList: ArrayList<Marker> = ArrayList()
private var throughPointMarkerVisible = true
private var mPolylineOptions: PolylineOptions? = null
private var mPolylineOptionsColor: PolylineOptions? = null
init {
try {
mRouteSearch = RouteSearchV2(context)
} catch (e: AMapException) {
e.printStackTrace()
}
}
fun setThroughPointList(throughPoints: List<LatLng>) {
if (throughPoints.size > 6) {
throughPointList = throughPoints.take(6)
return
}
throughPointList = throughPoints
}
fun setIsColorfulline(iscolorfulline: Boolean) {
isColorfulline = iscolorfulline
}
fun setThroughPointMarkerVisible(visible: Boolean) {
try {
throughPointMarkerVisible = visible
if (this.throughPointMarkerList.isNotEmpty()) {
for (i in this.throughPointMarkerList.indices) {
this.throughPointMarkerList[i].isVisible = visible
}
}
} catch (e: Throwable) {
e.printStackTrace()
}
}
override fun add(map: AMap) {
this.map = map
searchRouteResult()
}
override fun remove() {
removeMarkerFromMap()
removeLineFromMap()
try {
if (throughPointMarkerList.size > 0) {
for (i in throughPointMarkerList.indices) {
throughPointMarkerList[i].remove()
}
throughPointMarkerList.clear()
}
} catch (e: Throwable) {
e.printStackTrace()
}
}
private fun addToMap() {
initPolylineOptions()
if (getRouteWidth() == 0f || drivePath == null) {
return
}
val mLatLngsOfPath = ArrayList<LatLng>()
val tmcs = ArrayList<TMC>()
val drivePaths = drivePath?.steps
for (step in drivePaths!!) {
val latlonPoints = step.polyline
val tmclist = step.tmCs
tmcs.addAll(tmclist)
addDrivingStationMarkers(step, latlonPoints[0].convertToLatLng())
for (latlonpoint in latlonPoints) {
mPolylineOptions?.add(latlonpoint.convertToLatLng())
mLatLngsOfPath.add(latlonpoint.convertToLatLng())
}
}
addStartAndEndMarker()
addThroughPointMarker()
if (isColorfulline && tmcs.size > 0) {
colorWayUpdate(tmcs)
addPolyLine(mPolylineOptionsColor)
} else {
addPolyLine(mPolylineOptions)
}
}
/**
* 初始化线段属性
*/
private fun initPolylineOptions() {
mPolylineOptions = PolylineOptions()
.color(getRoadColor())
.setCustomTexture(getRoadLine())
.width(getRouteWidth())
}
/**
* 根据不同的路段拥堵情况展示不同的颜色
*
* @param tmcSection
*/
private fun colorWayUpdate(tmcSection: List<TMC>) {
var segmentTrafficStatus: TMC
mPolylineOptionsColor = PolylineOptions().width(getRouteWidth())
mPolylineOptionsColor?.add(tmcSection[0].polyline[0].convertToLatLng())
val colorList: MutableList<Int> = ArrayList()
colorList.add(getRoadColor())
for (i in tmcSection.indices) {
segmentTrafficStatus = tmcSection[i]
val color: Int = getColor(segmentTrafficStatus.status)
val mployline = segmentTrafficStatus.polyline
for (j in 1 until mployline.size) {
mPolylineOptionsColor?.add(mployline[j].convertToLatLng())
colorList.add(color)
}
}
colorList.add(getRoadColor())
mPolylineOptionsColor?.colorValues(colorList)
}
private fun getColor(status: String): Int {
return when (status) {
"畅通" -> {
Color.GREEN
}
"缓行" -> {
Color.YELLOW
}
"拥堵" -> {
Color.RED
}
"严重拥堵" -> {
Color.parseColor("#990033")
}
else -> {
Color.parseColor("#537edc")
}
}
}
/**
* @param driveStep
* @param latLng
*/
private fun addDrivingStationMarkers(driveStep: DriveStepV2, latLng: LatLng) {
addStationMarker(
MarkerOptions()
.position(latLng)
.title("\u65B9\u5411:" + driveStep.instruction + "\n\u9053\u8DEF:" + driveStep.road)
.snippet(driveStep.instruction).visible(nodeIconVisible)
.anchor(0.5f, 0.5f).icon(getDriveBit())
)
}
/**
* 添加途经点marker
*/
private fun addThroughPointMarker() {
if (throughPointList.isNotEmpty()) {
for (i in throughPointList.indices) {
val latLonPoint = throughPointList[i]
throughPointMarkerList.add(
map.addMarker(
MarkerOptions()
.position(latLonPoint)
.visible(throughPointMarkerVisible)
.icon(getThroughBit())
.title("\u9014\u7ECF\u70B9")
)
)
}
}
}
override fun searchRouteResult() {
if (mRouteSearch == null) {
return
}
val fromAndTo = createFromAndTo() ?: return
try {
showProgressDialog()
val driveRouteResult = mRouteSearch!!.calculateDriveRoute(
RouteSearchV2.DriveRouteQuery(
fromAndTo,
RouteSearchV2.DrivingStrategy.DEFAULT,
throughPointList.map { r -> r.convertToLatLonPoint() },
null,
null
)
)
onDriveRouteSearched(driveRouteResult)
} catch (e: AMapException) {
dissmissProgressDialog()
showerror(context, e.errorCode)
}
}
private fun onDriveRouteSearched(result: DriveRouteResultV2) {
dissmissProgressDialog()
map.clear()
if (result.paths != null) {
if (result.paths.size > 0) {
drivePath = result.paths[0] ?: return
remove()
addToMap()
zoomToSpan()
} else if (result.paths == null) {
show(context, R.string.no_result)
}
} else {
show(context, R.string.no_result)
}
}
fun searchRoute(args: ReadableArray?) {
val start = args?.getMap(0)?.toLatLonPoint()
val end = args?.getMap(1)?.toLatLonPoint()
val through = args?.getArray(2)?.toLatLngList()
if (start == null) {
Toast.makeText(context, "起点不能为空", Toast.LENGTH_LONG).show()
return
}
if (end == null) {
Toast.makeText(context, "终点不能为空", Toast.LENGTH_LONG).show()
return
}
setStartPoint(start)
setEndPoint(end)
if (through?.isNotEmpty() == true) {
setThroughPointList(through)
}
searchRouteResult()
}
}