puzhibing
2026-08-21 5fa94b3718b4a78a4fcea4c97652b0ef20b7de3a
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
package com.xw.yunwei.ui.work.checkInMission
 
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import com.amap.api.maps.AMap
import com.amap.api.maps.AMapUtils
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.CameraPosition
import com.amap.api.maps.model.CircleOptions
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.MarkerOptions
import com.amap.api.maps.model.Polyline
import com.amap.api.maps.model.PolylineOptions
import com.amap.api.navi.AMapNavi
import com.amap.api.navi.AMapNaviIndependentRouteListener
import com.amap.api.navi.AMapNaviListener
import com.amap.api.navi.TTSPlayListener
import com.amap.api.navi.enums.NaviType
import com.amap.api.navi.enums.TransportType
import com.amap.api.navi.enums.TravelStrategy
import com.amap.api.navi.model.AMapCalcRouteResult
import com.amap.api.navi.model.AMapLaneInfo
import com.amap.api.navi.model.AMapModelCross
import com.amap.api.navi.model.AMapNaviCameraInfo
import com.amap.api.navi.model.AMapNaviCross
import com.amap.api.navi.model.AMapNaviLocation
import com.amap.api.navi.model.AMapNaviPath
import com.amap.api.navi.model.AMapNaviPathGroup
import com.amap.api.navi.model.AMapNaviRouteNotifyData
import com.amap.api.navi.model.AMapNaviTrafficFacilityInfo
import com.amap.api.navi.model.AMapServiceAreaInfo
import com.amap.api.navi.model.AMapTravelInfo
import com.amap.api.navi.model.AimLessModeCongestionInfo
import com.amap.api.navi.model.AimLessModeStat
import com.amap.api.navi.model.NaviInfo
import com.amap.api.navi.model.NaviLatLng
import com.amap.api.navi.model.NaviPoi
import com.amap.api.navi.model.RouteOverlayOptions
import com.amap.api.navi.view.RouteOverLay
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SpanUtils
import com.google.gson.reflect.TypeToken
import com.xw.common.CommonDialog
import com.xw.common.utils.BindUtils.getResColor
import com.xw.common.utils.ToastUtilCommon
import com.xw.yunwei.BR
import com.xw.yunwei.BaseBusinessActivity
import com.xw.yunwei.R
import com.xw.yunwei.constants.Key
import com.xw.yunwei.databinding.ActivityCruiseCheckInBinding
import com.xw.yunwei.extension.colorIdToColorValue
import com.xw.yunwei.model.CheckInMissionPointInfo
import com.xw.yunwei.model.CruiseCheckInOperationResult
import com.xw.yunwei.ui.work.amap.custom.view.CarOverlay
import com.xw.yunwei.ui.work.amap.util.NaviUtil
import com.xw.yunwei.ui.work.checkInMission.util.Utils
import com.xw.yunwei.ui.work.checkInMission.viewmodel.CruiseCheckInViewModel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.text.SimpleDateFormat
import java.util.Date
 
//巡游打卡导航页
class CruiseCheckInActivity :
    BaseBusinessActivity<ActivityCruiseCheckInBinding, CruiseCheckInViewModel>(),
    AMapNaviListener, TTSPlayListener, AMapNaviIndependentRouteListener {
    companion object {
        //获取跳转此页面参数
        fun getParams(
            id: String,
            points: List<CheckInMissionPointInfo>,
            startPoint: NaviPoi,
            type: Int,
        ): Bundle {
            return bundleOf(
                Key.ID to id,
                Key.POINTS to GsonUtils.toJson(points),
                Key.START_POINT to startPoint,
                Key.TYPE to type
            )
        }
    }
 
    //导航对象
    var aMapNavi: AMapNavi? = null
 
    //设备位置类
    var carOverlay: CarOverlay? = null
 
    //路线OverLay
    var routeOverLay: RouteOverLay? = null
 
    //起点/终点回拉线
    private val leaderLines = mutableListOf<Polyline>()
 
    //上次计算路线起点定位
    var lastCalculateRouteStartLocation: AMapNaviLocation? = null
    override fun getBindingVariable() = BR.viewModel
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
 
        binding.amapView.onCreate(savedInstanceState)
 
        initAMapView()
 
        aMapNavi = AMapNavi.getInstance(this)?.also {
            it.isNaviTravelView = true
            it.addAMapNaviListener(this)
            it.addTTSPlayListener(this)
            it.setUseInnerVoice(true, true)
            it.setEmulatorNaviSpeed(75)
        }
 
        viewModel.id = intent.getStringExtra(Key.ID) ?: ""
        viewModel.points.postValue(
            GsonUtils.fromJson(
                intent.getStringExtra(Key.POINTS),
                object : TypeToken<List<CheckInMissionPointInfo>>() {}.type
            )
        )
        viewModel.startPoint = intent.getParcelableExtra(Key.START_POINT)
        viewModel.type = intent.getIntExtra(Key.TYPE, 0)
 
        viewModel.checkOutTimeOut()
    }
 
    //初始化高德导航控件
    private fun initAMapView() {
        binding.amapView.map?.also { it ->
            it.mapType = AMap.MAP_TYPE_NAVI
 
            it.uiSettings?.setZoomInByScreenCenter(false)
            it.uiSettings?.isZoomControlsEnabled = false
            it.setOnMapLoadedListener {
                it.setPointToCenter(
                    binding.amapView.width / 2,
                    binding.amapView.height / 2
                )
                it.moveCamera(CameraUpdateFactory.zoomTo(16f))
                carOverlay = CarOverlay(this, binding.amapView).apply {
                    setLock(false)
                }
                viewModel.needCalculateRoute.postValue(true)
            }
            it.setOnMarkerClickListener { marker ->
                if (binding.slFloatNotice.isVisible) {
                    return@setOnMarkerClickListener false
                }
                val poi = marker.`object` as NaviPoi?
                viewModel.points.value?.find {
                    it.lat?.toDoubleOrNull() == poi?.coordinate?.latitude &&
                            it.lon?.toDoubleOrNull() == poi?.coordinate?.longitude
                }?.also {
                    viewModel.choicePoint.postValue(it)
                }
                return@setOnMarkerClickListener true
            }
        }
    }
 
    override fun setObservable() {
        super.setObservable()
        viewModel.needCalculateRoute.observe(this) { it ->
            if (!it || viewModel.getEndPoint() == null) {
                return@observe
            }
            aMapNavi?.also { it ->
                if (viewModel.type == 0) {
                    it.travelInfo = AMapTravelInfo(TransportType.Walk)
                    val success = it.calculateWalkRoute(
                        viewModel.startPoint,
                        viewModel.getPassingPoints(),
                        viewModel.getEndPoint()!!,
                        TravelStrategy.SINGLE
                    )
                    if (!success) {
                        Log.d(TAG, "步行路线计算失败,检查参数情况")
                    }
                }
                if (viewModel.type == 1) {
                    it.travelInfo = AMapTravelInfo(TransportType.Ride)
                    val success = it.calculateRideRoute(
                        viewModel.startPoint,
                        viewModel.getPassingPoints(),
                        viewModel.getEndPoint()!!,
                        TravelStrategy.SINGLE
                    )
                    if (!success) {
                        Log.d(TAG, "骑行路线计算失败,检查参数情况")
                    }
                }
            }
        }
        viewModel.points.observe(this) {
            refreshNextPointInfo()
        }
        viewModel.isOverview.observe(this) { it ->
            binding.ivOverview.setImageResource(if (it) R.mipmap.ic_route_arrow_close else R.mipmap.ic_route_arrow)
            binding.tvOverview.text = if (it) "退出全览" else "全览"
            if (it) {
                routeOverLay?.zoomToSpan()
            } else {
                binding.amapView.map?.also {
                    viewModel.aMapNaviLocation?.also { location ->
                        it.animateCamera(
                            CameraUpdateFactory.newCameraPosition(
                                CameraPosition.fromLatLngZoom(
                                    LatLng(
                                        location.coord.latitude,
                                        location.coord.longitude
                                    ), 16f
                                )
                            )
                        )
                    }
                }
            }
        }
        viewModel.hadExpand.observe(this) {
            binding.ivFlexing.setImageResource(if (it) R.mipmap.ic_down else R.mipmap.ic_up)
            binding.llLocationInfo.visibility = if (it) View.VISIBLE else View.GONE
        }
        viewModel.couldCheckIn.observe(this) {
            binding.tvCheckIn.setBackgroundResource(if (it) R.drawable.shape_blue_5_bg_4 else R.drawable.shape_gray_4_bg_4)
            binding.tvCheckInNotice.visibility = if (it) View.INVISIBLE else View.VISIBLE
            binding.tvFloatCheckIn.setBackgroundResource(if (it) R.drawable.shape_blue_5_bg_4 else R.drawable.shape_gray_4_bg_4)
        }
        viewModel.checkInOperationInfo.observe(this) {
            viewModel.getNextPoint()?.also { nextPoint ->
                viewModel.couldCheckIn.postValue(it.isLocationInCheckInRange())
                if (!it.isEffective()) {
                    binding.llTimeoutNotice.visibility = View.GONE
                    binding.slFloatNotice.visibility = View.GONE
                    return@observe
                }
                binding.tvTime.text = nextPoint.time
                binding.tvAddress.text = nextPoint.address
 
                if (it.needShowCheckInNotice() && !viewModel.showCheckInNoticeHistory.contains(it.checkLocationId)) {
                    viewModel.showCheckInNoticeHistory.add(it.checkLocationId!!)
                    viewModel.choicePoint.postValue(null)
                    viewModel.hadExpand.postValue(false)
                    binding.slFloatNotice.visibility = View.VISIBLE
                    SpanUtils.with(binding.tvFloatNotice)
                        .append("您已到达")
                        .setForegroundColor(getResColor(com.xw.common.R.color.gray_9))
                        .append("【${it.message ?: nextPoint.address ?: ""}】")
                        .setForegroundColor(getResColor(com.xw.common.R.color.blue_5))
                        .append("打卡点,是否立即打卡?")
                        .setForegroundColor(getResColor(com.xw.common.R.color.gray_9))
                        .create()
                }
                if (it.needShowTimeoutNotice()) {
                    binding.tvTimeoutNotice.text = it.message
                    binding.llTimeoutNotice.visibility = View.VISIBLE
                    binding.llTimeoutNotice.postDelayed(Runnable {
                        if (isFinishing || isDestroyed) {
                            return@Runnable
                        }
                        binding.ivCloseTimeoutNotice.performClick()
                    }, 3000)
                }
            }
        }
        viewModel.choicePoint.observe(this) {
            if (it == null) {
                binding.llClickLocationInfo.visibility = View.GONE
                binding.ivFlexing.visibility = View.VISIBLE
                binding.tvClickLocationCheckIn.setOnClickListener(null)
                return@observe
            }
            binding.llClickLocationInfo.visibility = View.VISIBLE
            binding.ivFlexing.visibility = View.GONE
            binding.tvClickLocationTime.text = it.time
            binding.tvClickLocationAddress.text = it.address
            if (it.locationId == viewModel.checkInOperationInfo.value?.checkLocationId) {
                binding.tvClickLocationCheckIn.setBackgroundResource(R.drawable.shape_blue_5_bg_4)
                binding.tvClickLocationCheckInNotice.visibility = View.INVISIBLE
            } else {
                binding.tvClickLocationCheckIn.setBackgroundResource(R.drawable.shape_gray_4_bg_4)
                binding.tvClickLocationCheckInNotice.visibility = View.VISIBLE
            }
            binding.tvClickLocationCheckIn.setOnClickListener { _ ->
                binding.llClickLocationInfo.visibility = View.GONE
                binding.ivFlexing.visibility = View.VISIBLE
                binding.tvClickLocationCheckIn.setOnClickListener(null)
                viewModel.jumpToCheckInPage(it)
            }
        }
    }
 
    //刷新下一个打卡点信息
    private fun refreshNextPointInfo() {
        viewModel.getNextPoint()?.also {
            binding.tvTime.text = it.time
            binding.tvAddress.text = it.address
        }
    }
 
    //刷新预计耗时和剩余距离
    private fun refreshCostDurationDistance(path: AMapNaviPath) {
        binding.tvDistanceCostTime.text =
            "${Utils.getFriendlyDistance(path.allLength)} ${
                Utils.getFriendlyTime(path.allTime)
            }"
        val arrivalTime = System.currentTimeMillis() + (path.allTime * 1000)
        binding.tvEstimatedTimeOfArrival.text =
            "${SimpleDateFormat("HH:mm").format(Date(arrivalTime))}到达"
    }
 
    override fun setListener() {
        super.setListener()
        binding.llExit.setOnClickListener {
            CommonDialog.showTipsDialog(
                context = this,
                content = "是否退出导航?",
                rightText = "确认退出",
                rightBlock = {
                    finish()
                    return@showTipsDialog true
                })
        }
        binding.llOverview.setOnClickListener {
            viewModel.isOverview.postValue(!(viewModel.isOverview.value ?: false))
        }
        binding.ivFlexing.setOnClickListener {
            if (binding.slFloatNotice.isVisible) {
                return@setOnClickListener
            }
            viewModel.hadExpand.postValue(!(viewModel.hadExpand.value ?: false))
            //binding.slFloatNotice.visibility = View.GONE
        }
        binding.tvFloatDelayCheckIn.setOnClickListener { _ ->
            binding.slFloatNotice.visibility = View.GONE
            binding.ivFlexing.performClick()
        }
        binding.tvFloatCheckIn.setOnClickListener { _ ->
            binding.slFloatNotice.visibility = View.GONE
            viewModel.getNextPoint()?.also { nextPoint ->
                viewModel.jumpToCheckInPage(nextPoint)
            }
        }
        binding.tvCheckIn.setOnClickListener {
            viewModel.getNextPoint()?.also { nextPoint ->
                viewModel.jumpToCheckInPage(nextPoint)
            }
        }
        binding.ivCloseTimeoutNotice.setOnClickListener {
            binding.llTimeoutNotice.animate()
                .alpha(0f)
                .setDuration(300) // 动画时长(毫秒)
                .withEndAction {
                    binding.llTimeoutNotice.visibility = View.GONE // 动画结束后隐藏控件
                    binding.llTimeoutNotice.alpha = 1f
                }
                .start()
        }
        binding.llClickLocationInfoBack.setOnClickListener {
            viewModel.choicePoint.postValue(null)
        }
    }
 
    override fun onResume() {
        super.onResume()
        binding.amapView.onResume()
    }
 
    override fun onPause() {
        super.onPause()
        binding.amapView.onPause()
    }
 
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        binding.amapView.onSaveInstanceState(outState)
    }
 
    override fun onBackPressed() {
        //super.onBackPressed()
        binding.llExit.performClick()
    }
 
    //清除地图上的路线
    private fun clearRouteFromMap() {
        routeOverLay?.also {
            it.removeFromMap()
            it.destroy()
        }
        routeOverLay = null
    }
 
    override fun onDestroy() {
        super.onDestroy()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
        clearRouteFromMap()
        carOverlay?.also {
            it.destroy()
        }
        carOverlay = null
        binding.amapView.onDestroy()
        aMapNavi?.also {
            it.removeAMapNaviListener(this)
            it.removeTTSPlayListener(this)
            it.stopNavi()
            AMapNavi.destroy()
        }
    }
 
    override fun onInitNaviFailure() {
 
    }
 
    override fun onInitNaviSuccess() {
 
    }
 
    override fun onStartNavi(p0: Int) {
 
    }
 
    override fun onTrafficStatusUpdate() {
 
    }
 
    override fun onLocationChange(p0: AMapNaviLocation?) {
        p0?.also { location ->
            carOverlay?.also { it ->
                viewModel.aMapNaviLocation = location
                val latLng = LatLng(location.coord.latitude, location.coord.longitude)
                it.draw(binding.amapView.map, latLng, location.bearing)
            }
            independentCalculateRoute()
        }
        refreshNextPointInfo()
    }
 
    override fun onGetNavigationText(p0: Int, p1: String?) {
 
    }
 
    override fun onGetNavigationText(p0: String?) {
 
    }
 
    override fun onEndEmulatorNavi() {
 
    }
 
    override fun onArriveDestination() {
 
    }
 
    override fun onCalculateRouteFailure(p0: Int) {
        Log.d(TAG, "${if (viewModel.type == 0) "步行" else "骑行"}路线计算失败,$p0")
        ToastUtilCommon.showError("${if (viewModel.type == 0) "步行" else "骑行"}路线计算失败")
    }
 
    override fun onReCalculateRouteForYaw() {
 
    }
 
    override fun onReCalculateRouteForTrafficJam() {
 
    }
 
    override fun onArrivedWayPoint(p0: Int) {
 
    }
 
    override fun onGpsOpenStatus(p0: Boolean) {
 
    }
 
    override fun onNaviInfoUpdate(p0: NaviInfo?) {
 
    }
 
    override fun updateCameraInfo(p0: Array<out AMapNaviCameraInfo?>?) {
 
    }
 
    override fun updateIntervalCameraInfo(
        p0: AMapNaviCameraInfo?,
        p1: AMapNaviCameraInfo?,
        p2: Int
    ) {
 
    }
 
    override fun onServiceAreaUpdate(p0: Array<out AMapServiceAreaInfo?>?) {
 
    }
 
    override fun showCross(p0: AMapNaviCross?) {
 
    }
 
    override fun hideCross() {
 
    }
 
    override fun showModeCross(p0: AMapModelCross?) {
 
    }
 
    override fun hideModeCross() {
 
    }
 
    override fun showLaneInfo(
        p0: Array<out AMapLaneInfo?>?,
        p1: ByteArray?,
        p2: ByteArray?
    ) {
 
    }
 
    override fun showLaneInfo(p0: AMapLaneInfo?) {
 
    }
 
    override fun hideLaneInfo() {
 
    }
 
    override fun onCalculateRouteSuccess(p0: IntArray?) {
 
    }
 
    override fun notifyParallelRoad(p0: Int) {
 
    }
 
    override fun OnUpdateTrafficFacility(p0: Array<out AMapNaviTrafficFacilityInfo?>?) {
 
    }
 
    override fun OnUpdateTrafficFacility(p0: AMapNaviTrafficFacilityInfo?) {
 
    }
 
    override fun updateAimlessModeStatistics(p0: AimLessModeStat?) {
 
    }
 
    override fun updateAimlessModeCongestionInfo(p0: AimLessModeCongestionInfo?) {
 
    }
 
    override fun onPlayRing(p0: Int) {
 
    }
 
    override fun onCalculateRouteSuccess(p0: AMapCalcRouteResult?) {
        Log.d(TAG, "${if (viewModel.type == 0) "步行" else "骑行"}路线计算成功,$p0")
        aMapNavi?.naviPath?.takeIf {
            !isFinishing && !isDestroyed
        }?.also { path ->
            clearRouteFromMap()
            binding.amapView.map?.clear()
            refreshCostDurationDistance(path)
            initRouteOverlay(path)
            initArea()
            initMarker()
            initCarOverlay(path)
            // 开启导航
            aMapNavi?.startNavi(NaviType.GPS)
            binding.amapView.postDelayed({
                if (isFinishing || isDestroyed) {
                    return@postDelayed
                }
                viewModel.isOverview.postValue(false)
            }, 100L)
        }
    }
 
    //初始化路线
    private fun initRouteOverlay(path: AMapNaviPath) {
        routeOverLay = RouteOverLay(binding.amapView.map, path, this).apply {
            setTrafficLine(true)
            setLightsVisible(false)
            setStartPointBitmap(null)
            setEndPointBitmap(null)
            setWayPointBitmap(null)
            showRouteStart(false)
            showRouteEnd(false)
//                setPassRouteVisible(false)
            routeOverlayOptions = RouteOverlayOptions().apply {
                setOnRouteCameShow(false)
            }
            addToMap()
        }
        drawLeaderLines(path)
    }
 
    //绘制起点/终点的回拉线(人到路线起点、路线终点到建筑内终点)
    private fun drawLeaderLines(naviPath: AMapNaviPath) {
        binding.amapView.map?.also { aMap ->
            leaderLines.forEach { it.remove() }
            leaderLines.clear()
 
            val coordList = naviPath.coordList ?: return
            if (coordList.isEmpty()) return
            val routeStart = LatLng(coordList.first().latitude, coordList.first().longitude)
            val routeEnd = LatLng(coordList.last().latitude, coordList.last().longitude)
            val lineColor = getResColor(com.xw.common.R.color.gray_6)
 
            viewModel.startPoint?.coordinate?.also { startLatLng ->
                if (AMapUtils.calculateLineDistance(startLatLng, routeStart) > 5) {
                    aMap.addPolyline(
                        PolylineOptions()
                            .add(startLatLng, routeStart)
                            .width(13f)
                            .color(lineColor)
                            .setDottedLine(true)
                    )?.also { leaderLines.add(it) }
                }
            }
 
            viewModel.getEndPoint()?.coordinate?.also { endLatLng ->
                if (AMapUtils.calculateLineDistance(routeEnd, endLatLng) > 5) {
                    aMap.addPolyline(
                        PolylineOptions()
                            .add(routeEnd, endLatLng)
                            .width(13f)
                            .color(lineColor)
                            .setDottedLine(true)
                    )?.also { leaderLines.add(it) }
                }
            }
        }
    }
 
    //初始化marker
    private fun initMarker() {
        binding.amapView.map?.also {
//            viewModel.startPoint?.also { startPoint ->
//                it.addMarker(
//                    MarkerOptions()
//                        .position(
//                            LatLng(
//                                startPoint.coordinate.latitude,
//                                startPoint.coordinate.longitude
//                            )
//                        )
//                        .icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_start_point))
//                        .anchor(0.5f, 0.5f)
//                )?.apply {
//                    isClickable = true
//                    `object` = startPoint
//                }
//            }
            viewModel.getEndPoint()?.also { endPoint ->
                it.addMarker(
                    MarkerOptions()
                        .position(
                            LatLng(
                                endPoint.coordinate.latitude,
                                endPoint.coordinate.longitude
                            )
                        )
                        .icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_end_point))
                        .anchor(0.5f, 0.5f)
                )?.apply {
                    isClickable = true
                    `object` = endPoint
                }
            }
            viewModel.getPassingPoints().forEach { point ->
                it.addMarker(
                    MarkerOptions()
                        .position(
                            LatLng(
                                point.coordinate.latitude,
                                point.coordinate.longitude
                            )
                        )
                        .icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_pass_point))
                        .anchor(0.5f, 0.5f)
                )?.apply {
                    isClickable = true
                    `object` = point
                }
            }
        }
    }
 
    //初始化范围
    private fun initArea() {
        binding.amapView.map?.also { it ->
            viewModel.points.value?.filter { !it.hadCheckIn() }?.forEach { point ->
                it.addCircle(CircleOptions().apply {
                    center(
                        LatLng(
                            point.lat?.toDoubleOrNull() ?: 0.0,
                            point.lon?.toDoubleOrNull() ?: 0.0
                        )
                    )
                    radius(point.openRange?.toDoubleOrNull() ?: 0.0)
                    fillColor(R.color.color_1a026ee4.colorIdToColorValue(application))
                    strokeColor(Color.TRANSPARENT)
                })
            }
        }
    }
 
    //初始化车标
    private fun initCarOverlay(path: AMapNaviPath) {
        carOverlay?.also { it ->
            it.reset()
            path.endPoint?.also { end ->
                it.setEndPoi(LatLng(end.latitude, end.longitude))
            }
            val latLng = viewModel.aMapNaviLocation?.let {
                NaviLatLng(it.coord.latitude, it.coord.longitude)
            } ?: NaviLatLng(
                viewModel.startPoint!!.coordinate.latitude,
                viewModel.startPoint!!.coordinate.longitude
            )
            val rotate = NaviUtil.getRotate(latLng, path.coordList.first())
            it.draw(binding.amapView.map, viewModel.startPoint!!.coordinate, rotate)
        }
    }
 
    //POI独立算路
    //独立路径规划是指路径规划的结果不会自动应用于当前导航,也不会干扰当前的导航
    private fun independentCalculateRoute() {
        viewModel.aMapNaviLocation?.takeIf {
            (lastCalculateRouteStartLocation == null || NaviUtil.calculateDistance(
                lastCalculateRouteStartLocation!!.coord,
                it.coord
            ) > 10) && viewModel.getEndPoint() != null
        }?.also { location ->
            aMapNavi?.also {
                when (viewModel.type) {
                    0, 1 -> {
                        lastCalculateRouteStartLocation = location
                        val success = it.independentCalculateRoute(
                            NaviPoi(
                                "起点",
                                LatLng(location.coord.latitude, location.coord.longitude),
                                ""
                            ),
                            viewModel.getEndPoint()!!,
                            viewModel.getPassingPoints(),
                            TravelStrategy.SINGLE.value,
                            if (viewModel.type == 0) TransportType.Walk else TransportType.Ride,
                            this
                        )
                        if (!success) {
                            Log.d(
                                TAG,
                                "${if (viewModel.type == 0) "步行" else "骑行"}独立算路失败,检查参数情况"
                            )
                        }
                    }
                }
            }
        }
    }
 
    override fun onCalculateRouteFailure(p0: AMapCalcRouteResult?) {
 
    }
 
    override fun onNaviRouteNotify(p0: AMapNaviRouteNotifyData?) {
 
    }
 
    override fun onGpsSignalWeak(p0: Boolean) {
 
    }
 
    override fun onPlayStart(p0: String?) {
        viewModel.hadPlayTTS = true
    }
 
    override fun onPlayEnd(p0: String?) {
        viewModel.hadPlayTTS = false
    }
 
    override fun onIndependentCalculateSuccess(p0: AMapNaviPathGroup?) {
        Log.d(TAG, "${if (viewModel.type == 0) "步行" else "骑行"}独立算路成功${p0}")
        p0?.takeIf {
            it.mainPath != null && !isFinishing && !isDestroyed
        }?.also {
            refreshCostDurationDistance(it.mainPath)
            clearRouteFromMap()
            initRouteOverlay(it.mainPath)
        }
    }
 
    override fun onIndependentCalculateFail(p0: AMapCalcRouteResult?) {
        Log.d(TAG, "${if (viewModel.type == 0) "步行" else "骑行"}独立算路失败,$p0")
    }
 
    override fun onIndependentCalculateInvoke(p0: Int) {
 
    }
 
    @Suppress("unused")
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: CruiseCheckInOperationResult) {
        if (event.cruiseCheckInRuleId != viewModel.id) {
            return
        }
        viewModel.refreshPoints(event.checkLocationId, event.checkIn, event.checkInResult)
    }
}