[bus][shuttle][passenger]

[代码整理][地图抽取]
This commit is contained in:
yangyakun
2024-02-04 16:56:53 +08:00
parent 7d10603da6
commit c3c8bad874
42 changed files with 343 additions and 1928 deletions

View File

@@ -23,7 +23,7 @@ import com.mogo.och.shuttle.passenger.ui.adapter.BusPassengerLineStationsAdapter
import com.mogo.och.shuttle.passenger.constant.BusPassengerConst;
import com.mogo.och.shuttle.passenger.presenter.BaseBusPassengerPresenter;
import com.mogo.och.shuttle.passenger.ui.layoutmanager.CenterLayoutManager;
import com.mogo.och.shuttle.passenger.ui.mapdirectionview.BusPassengerMapDirectionView;
import com.mogo.och.common.module.wigets.mapdirectionview.MapDirectionView;
import com.mogo.och.common.module.utils.NumberFormatUtil;
import com.mogo.och.common.module.wigets.MarqueeTextView;
import com.mogo.och.common.module.wigets.OCHGradientTextView;
@@ -48,7 +48,7 @@ public class BusPassengerRouteFragment extends
private MarqueeTextView mLineName;
private Group mRouteInfoView;
private RecyclerView mStationsListRv;
private BusPassengerMapDirectionView mMapDirectionView;
private MapDirectionView mMapDirectionView;
private BusPassengerLineStationsAdapter mAdapter;
private TextView emptyTv;
private AppCompatImageView mAutopilotIv;
@@ -157,7 +157,6 @@ public class BusPassengerRouteFragment extends
@Override
public void run() {
mMapDirectionView.clearLineMarkers();
}
});
}

View File

@@ -1,354 +0,0 @@
package com.mogo.och.shuttle.passenger.ui.mapdirectionview
import android.content.Context
import android.os.Bundle
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.findViewTreeViewModelStoreOwner
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.TextureMapView
import com.amap.api.maps.model.BitmapDescriptor
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.CameraPosition
import com.amap.api.maps.model.CustomMapStyleOptions
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.LatLngBounds
import com.amap.api.maps.model.Marker
import com.amap.api.maps.model.MarkerOptions
import com.amap.api.maps.model.Polyline
import com.amap.api.maps.model.PolylineOptions
import com.mogo.eagle.core.data.map.MogoLocation
import com.mogo.eagle.core.function.api.autopilot.IMoGoChassisLocationGCJ02Listener
import com.mogo.eagle.core.function.call.autopilot.CallerChassisLocationGCJ02ListenerManager
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger.d
import com.mogo.eagle.core.utilcode.mogo.logger.scene.SceneConstant
import com.mogo.eagle.core.utilcode.util.UiThreadHandler
import com.mogo.och.shuttle.passenger.R
import com.mogo.och.shuttle.passenger.ui.IBusPassengerMapDirectionView
import com.mogo.och.shuttle.passenger.utils.BusPassengerMapAssetStyleUtil
/**
* 乘客屏小地图
*/
class BusPassengerMapDirectionView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr), IMoGoChassisLocationGCJ02Listener,
IBusPassengerMapDirectionView, AMap.OnCameraChangeListener,
MapDirectionViewModel.ItineraryViewCallback {
companion object {
//小地图名称
const val TAG = "BusPassengerMapDirectionView"
}
private lateinit var mAMapNaviView: TextureMapView
private lateinit var mAMap: AMap
private var mPolyline: Polyline? = null
private val mLineMarkers: MutableList<Marker> = ArrayList()
private lateinit var mCarMarker: Marker
private val mLineStationLatLng: MutableList<LatLng> = ArrayList() //站点坐标数据
var textureList: MutableList<BitmapDescriptor?> = ArrayList()
var texIndexList: MutableList<Int> = ArrayList()
private var mArrivedRes: BitmapDescriptor? = null
private var mUnArrivedRes: BitmapDescriptor? = null
private val routeArrived: MutableList<LatLng> = ArrayList()
private val routeArriving: MutableList<LatLng> = ArrayList()
private var location: MogoLocation? = null
init {
try {
initView(context)
} catch (e: Exception) {
e.printStackTrace()
}
}
private fun initView(context: Context) {
d(SceneConstant.M_BUS_P + TAG, "initView")
val smpView = LayoutInflater.from(context).inflate(R.layout.shuttle_p_jl_map_view, this)
mAMapNaviView = smpView.findViewById<View>(R.id.bus_p_line_amap_view) as TextureMapView
initAMapView()
// 注册定位监听
CallerChassisLocationGCJ02ListenerManager.addListener(TAG, 1, this)
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
// 注册定位监听
CallerChassisLocationGCJ02ListenerManager.removeListener(TAG)
}
private fun initAMapView() {
mAMap = mAMapNaviView.map
// 设置导航地图模式aMap是地图控制器对象。
mAMap.mapType = AMap.MAP_TYPE_NIGHT
// 关闭显示实时路况图层aMap是地图控制器对象。
mAMap.isTrafficEnabled = false
// 设置 锚点 图标
mCarMarker = mAMap.addMarker(
MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.shuttle_p_jl_map_car))
.anchor(0.5f, 0.5f)
)
mArrivedRes = BitmapDescriptorFactory.fromResource(R.drawable.shuttle_p_jl_map_arrow_arrived)
mUnArrivedRes = BitmapDescriptorFactory.fromResource(R.drawable.shuttle_p_jl_map_arrow_un_arrive)
// 加载自定义样式
val customMapStyleOptions = CustomMapStyleOptions()
.setEnable(true)
.setStyleData(
BusPassengerMapAssetStyleUtil.getAssetsStyle(
context, "map_style.data"
)
)
.setStyleExtraData(
BusPassengerMapAssetStyleUtil.getAssetsExtraStyle(
context, "map_style_extra.data"
)
)
// 设置自定义样式
mAMap.setCustomMapStyle(customMapStyleOptions)
// 设置地图的样式
mAMap.uiSettings.apply {
isZoomControlsEnabled = false // 地图缩放级别的交换按钮
setAllGesturesEnabled(true) // 所有手势
isMyLocationButtonEnabled = false // 显示默认的定位按钮
setLogoBottomMargin(-150) //设置Logo下边界距离屏幕底部的边距,设置为负值即可
}
mAMap.setOnMapLoadedListener {
d(SceneConstant.M_BUS_P + TAG, "smp---onMapLoaded")
// 加载自定义样式
val options = CustomMapStyleOptions()
.setEnable(true)
.setStyleData(
BusPassengerMapAssetStyleUtil.getAssetsStyle(
context, "map_style.data"
)
)
.setStyleExtraData(
BusPassengerMapAssetStyleUtil.getAssetsExtraStyle(
context, "map_style_extra.data"
)
)
// 设置自定义样式
mAMap.setCustomMapStyle(options)
mAMapNaviView.map.setPointToCenter(
mAMapNaviView.width / 2,
mAMapNaviView.height / 2
)
}
//设置地图状态的监听接口
mAMap.setOnCameraChangeListener(this)
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
val viewModel = findViewTreeViewModelStoreOwner()?.let {
ViewModelProvider(it).get(MapDirectionViewModel::class.java)
}
viewModel?.setDistanceCallback(this)
}
fun clearMapView() {
UiThreadHandler.post( {
clearPolyline()
clearCoordinatesLatLng()
}, UiThreadHandler.MODE.QUEUE)
}
override fun onChassisLocationGCJ02(mogoLocation: MogoLocation?) {
if (mogoLocation == null) {
return
}
val currentLatLng = LatLng(mogoLocation.latitude, mogoLocation.longitude)
//更新车辆位置
mCarMarker.rotateAngle = (360 - mogoLocation.heading).toFloat()
mCarMarker.position = currentLatLng
mCarMarker.setToTop()
try {
//圈定地图显示范围
val boundsBuilder = LatLngBounds.Builder()
routeArrived.forEach {
boundsBuilder.include(it)
}
routeArriving.forEach {
boundsBuilder.include(it)
}
mLineStationLatLng.forEach {
boundsBuilder.include(it)
}
boundsBuilder.include(currentLatLng)
mAMap.moveCamera(
CameraUpdateFactory.newLatLngBoundsRect(
boundsBuilder.build(),
100,
100,
100,
100
)
)
} catch (e: Exception) {
e.printStackTrace()
}
}
override fun drawablePolyline() {
if (routeArrived.isEmpty() && routeArriving.isEmpty()) {
d(SceneConstant.M_TAXI + TAG, "没有点")
return
}
try {
texIndexList.clear()
val allPoints = ArrayList(routeArrived)
for (i in routeArrived.indices) {
if (routeArrived.size > 1 && i < routeArrived.size - 1) {
texIndexList.add(0)
}
}
texIndexList.add(0)
location?.let {
allPoints.add(LatLng(it.latitude, it.longitude))
}
allPoints.addAll(routeArriving)
for (ignored in routeArrived) {
texIndexList.add(1)
}
mPolyline?.let {
it.points = allPoints
it.options.customTextureIndex = texIndexList
return
}
if (textureList.isEmpty()) {
textureList.add(mArrivedRes)
textureList.add(mUnArrivedRes)
}
//设置线段纹理
val polylineOptions = PolylineOptions().apply {
addAll(allPoints)
isUseTexture = true
width(15f)
lineCapType(PolylineOptions.LineCapType.LineCapRound)
customTextureList = textureList
customTextureIndex = texIndexList
}
// 绘制线
mPolyline = mAMap.addPolyline(polylineOptions)
} catch (e: Exception) {
e.printStackTrace()
}
}
override fun clearPolyline() {
mPolyline?.remove()
mPolyline = null
}
override fun setLineMarker() {}
fun clearCoordinatesLatLng() {
textureList.clear()
texIndexList.clear()
routeArrived.clear()
routeArriving.clear()
mLineStationLatLng.clear()
d(SceneConstant.M_BUS_P + TAG, " mCoordinatesLatLng.clear ")
}
fun onCreateView(savedInstanceState: Bundle?) {
mAMapNaviView.onCreate(savedInstanceState)
}
fun onResume() {
mAMapNaviView.onResume()
}
fun onPause() {
mAMapNaviView.onPause()
}
fun onDestroy() {
mAMapNaviView.onDestroy()
}
override fun setCoordinatesLatLng(
routeArrived: List<LatLng>,
routeArriving: List<LatLng>,
location: MogoLocation?
) {
this.routeArrived.clear()
this.routeArrived.addAll(routeArrived)
this.routeArriving.clear()
this.routeArriving.addAll(routeArriving)
this.location = location
UiThreadHandler.post({
drawablePolyline()
}, UiThreadHandler.MODE.QUEUE)
}
fun clearLineMarkers() {
for (i in mLineMarkers.indices) {
mLineMarkers[i].isVisible = false
mLineMarkers[i].remove()
}
mLineMarkers.clear()
}
fun setLinePointMarkerAndDraw(mLineStationsList: List<LatLng>, currentIndex: Int) {
clearLineMarkers()
mLineStationLatLng.clear()
mLineStationLatLng.addAll(mLineStationsList)
if (mLineStationsList.isNotEmpty()) {
// 起点marker, 终点marker, 过站marker, 未过站marker
val size = mLineStationsList.size
val mStartMarker = mAMap.addMarker(
MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.shuttle_p_jl_map_start_point))
)
val mEndMarker = mAMap.addMarker(
MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.shuttle_p_jl_map_end_point))
)
mStartMarker.position = mLineStationsList[0]
mLineMarkers.add(0, mStartMarker)
for (i in mLineStationsList.indices) {
if (currentIndex <= i && i < size - 1 && i > 0) { //未到达
val unArrivedMarker = mAMap.addMarker(
MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.shuttle_p_jl_map_unarrived_point))
)
unArrivedMarker.position = mLineStationsList[i]
mLineMarkers.add(i, unArrivedMarker)
} else if (i in 1 until currentIndex) {
val arrivedMarker = mAMap.addMarker(
MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.shuttle_p_jl_map_arrived_point))
)
arrivedMarker.position = mLineStationsList[i]
mLineMarkers.add(i, arrivedMarker)
}
}
mEndMarker.position = mLineStationsList[size - 1]
mLineMarkers.add(size - 1, mEndMarker)
}
}
override fun onCameraChange(cameraPosition: CameraPosition) {
}
override fun onCameraChangeFinish(cameraPosition: CameraPosition) {}
}

View File

@@ -1,60 +0,0 @@
package com.mogo.och.shuttle.passenger.ui.mapdirectionview
import androidx.lifecycle.ViewModel
import com.amap.api.maps.model.LatLng
import com.mogo.eagle.core.data.map.MogoLocation
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger
import com.mogo.och.common.module.manager.distancemamager.ITrajectoryListener
import com.mogo.och.common.module.manager.distancemamager.TrajectoryAndDistanceManager
class MapDirectionViewModel: ViewModel(), ITrajectoryListener {
private val TAG = MapDirectionViewModel::class.java.simpleName
private var viewCallback: ItineraryViewCallback?=null
init {
TrajectoryAndDistanceManager.addTrajectoryListener(TAG,this)
}
fun setDistanceCallback(viewCallback: ItineraryViewCallback){
this.viewCallback = viewCallback
}
override fun onCleared() {
super.onCleared()
this.viewCallback = null
TrajectoryAndDistanceManager.removeListener(TAG)
}
interface ItineraryViewCallback{
fun setCoordinatesLatLng(routeArrived: List<LatLng>, routeArriving: List<LatLng>, location: MogoLocation?)
}
override fun trajectoryCallback(
routeArrivied: MutableList<MogoLocation>,
routeArriving: MutableList<MogoLocation>,
location: MogoLocation
) {
val routeArrivedTemp: MutableList<LatLng> = ArrayList()
val routeArrivingTemp: MutableList<LatLng> = ArrayList()
var temp: LatLng
for (mogoLocation in routeArrivied) {
temp = LatLng(mogoLocation.latitude, mogoLocation.longitude)
routeArrivedTemp.add(temp)
}
for (mogoLocation in routeArriving) {
temp = LatLng(mogoLocation.latitude, mogoLocation.longitude)
routeArrivingTemp.add(temp)
}
this.viewCallback?.setCoordinatesLatLng(
routeArrivedTemp,
routeArrivingTemp,
location
)
CallerLogger.d(TAG,"已经走过的点routeArrivied:${routeArrivied.size} 未走过的点routeArriving${routeArriving.size}")
}
}