修复历史轨迹拖动滚动条宠物头像不会重新回到屏幕中心bug

This commit is contained in:
yezhiqiu
2026-02-05 15:31:35 +08:00
parent 7aab33bc12
commit 5373acaa80
6 changed files with 9 additions and 520 deletions

View File

@@ -1 +1 @@
#Wed Feb 04 14:33:32 CST 2026 #Thu Feb 05 15:21:25 CST 2026

View File

@@ -1,194 +0,0 @@
package com.abbidot.tracker.ui.common.map
import android.content.Context
import androidx.fragment.app.Fragment
import com.abbidot.baselibrary.constant.MMKVKey
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.tracker.base.BaseMapCommon
import com.abbidot.tracker.bean.HistoryDataBean
import com.abbidot.tracker.bean.MapDeviceBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.ui.fragment.map.baidumap.HomeMapBaiduMapFragment
import com.abbidot.tracker.ui.fragment.map.googlemap.HomeMapGoogleMapFragment
import com.abbidot.tracker.widget.MapDeviceNetView
import com.abbidot.tracker.widget.MapMarkerInfoView
import com.google.android.gms.maps.model.LatLng
import javax.inject.Inject
/**
*Created by .yzq on 2022/7/5/005.
* @link
* @description:
*/
class HomeMapCommon @Inject constructor() : BaseMapCommon() {
private var mHomeMapGoogleMapFragment: HomeMapGoogleMapFragment? = null
private var mHomeMapBaiduMapFragment: HomeMapBaiduMapFragment? = null
fun getMapFragment(
context: Context,
markerInfoView: MapMarkerInfoView,
mapDeviceNetView: MapDeviceNetView,
mapLoadOk: () -> Unit
): Fragment {
return if (AppUtils.isChina(AppUtils.SWITCH_MAP_TYPE)) {
mHomeMapBaiduMapFragment = HomeMapBaiduMapFragment.newInstance(context, mapLoadOk)
mHomeMapBaiduMapFragment!!
} else {
mHomeMapGoogleMapFragment = HomeMapGoogleMapFragment.newInstance(
context, markerInfoView, mapDeviceNetView, mapLoadOk
)
mHomeMapGoogleMapFragment!!
}
}
fun setMapDeviceBean(mapDeviceBean: MapDeviceBean?) {
if (null != mHomeMapBaiduMapFragment) {
mHomeMapBaiduMapFragment!!.setMapDeviceBean(mapDeviceBean)
} else if (null != mHomeMapGoogleMapFragment) {
mHomeMapGoogleMapFragment!!.setMapDeviceBean(mapDeviceBean)
}
}
/**
* 开始每隔几秒刷新用户位置
*/
fun startRefreshUserLocation() {
if (isMapLoadOk()) startRefreshUserLocation(
mHomeMapBaiduMapFragment, mHomeMapGoogleMapFragment
)
}
/**
* 获取宠物反编译好的位置信息
*/
fun getDecPetAddressData(): HistoryDataBean? {
if (null != mHomeMapBaiduMapFragment) {
return mHomeMapBaiduMapFragment!!.mDecPetAddressData
} else if (null != mHomeMapGoogleMapFragment) {
return mHomeMapGoogleMapFragment!!.mDecPetAddressData
}
return null
}
fun clearMarker() {
if (null != mHomeMapBaiduMapFragment) {
mHomeMapBaiduMapFragment!!.mBaiduMap?.clear()
} else if (null != mHomeMapGoogleMapFragment) {
mHomeMapGoogleMapFragment!!.clearAllMarker()
}
}
fun refreshPetCurrentLocation(latitude: Double, longitude: Double, isMoveCamera: Boolean) {
if (null != mHomeMapBaiduMapFragment) {
mHomeMapBaiduMapFragment?.apply {
refreshPetCurrentLocation(
latitude, longitude, isMoveCamera
)
}
} else if (null != mHomeMapGoogleMapFragment) {
MMKVUtil.putDouble(MMKVKey.MapShowDefaultLat, latitude)
MMKVUtil.putDouble(MMKVKey.MapShowDefaultLon, longitude)
mHomeMapGoogleMapFragment?.apply {
refreshPetCurrentLocation(
LatLng(latitude, longitude), needMoveCamera = isMoveCamera
)
}
}
}
/**
* 重新设置宠物头像
*/
fun setPetHeadIcon(imageUrl: String, petType: Int) {
if (null != mHomeMapBaiduMapFragment) {
mHomeMapBaiduMapFragment!!.setPetHeadIconUrl(imageUrl)
} else if (null != mHomeMapGoogleMapFragment) {
mHomeMapGoogleMapFragment!!.setPetHeadIconUrl(imageUrl, petType)
}
}
/**
* 地图是否加载好了,准备好了
*/
fun isMapLoadOk() = isMapLoadOk(mHomeMapBaiduMapFragment, mHomeMapGoogleMapFragment)
/**
* 切换地图类型
*/
fun switchSatelliteAndNormalMapType() {
switchSatelliteAndNormalMapType(mHomeMapBaiduMapFragment, mHomeMapGoogleMapFragment)
}
/**
* 切换用户和宠物的位置居中显示
*/
fun switchShowLocation(@ConstantInt type: Int) {
switchShowLocationType(type, mHomeMapBaiduMapFragment, mHomeMapGoogleMapFragment)
}
/**
* 画轨迹线
*/
fun addTrackLine(latitude: Double, longitude: Double) {
if (null != mHomeMapBaiduMapFragment) {
mHomeMapBaiduMapFragment!!.addLine(latitude, longitude)
} else if (null != mHomeMapGoogleMapFragment) {
mHomeMapGoogleMapFragment!!.addLine(LatLng(latitude, longitude))
}
}
/**
* 画轨迹线
*/
fun addTrackLines(trackList: MutableList<HistoryDataBean>) {
if (null != mHomeMapBaiduMapFragment) {
} else if (null != mHomeMapGoogleMapFragment) {
mHomeMapGoogleMapFragment!!.addLines(trackList)
}
}
/**
* 计算用户和宠物的距离
*/
fun getUserAndPetDistance(): Double {
var distance = 0.0
if (null != mHomeMapBaiduMapFragment) {
distance = mHomeMapBaiduMapFragment!!.getUserAndPetDistance()
} else if (null != mHomeMapGoogleMapFragment) {
distance = mHomeMapGoogleMapFragment!!.getUserAndPetDistance()
}
return distance
}
/**
* 开始水波纹动画
*/
fun startRippleCircleAnim() {
if (null != mHomeMapBaiduMapFragment) {
} else if (null != mHomeMapGoogleMapFragment) {
mHomeMapGoogleMapFragment!!.startRippleCircleAnim()
}
}
fun greenRippleCircleAnim() {
mHomeMapGoogleMapFragment?.greenRippleCircleAnim()
}
fun setMarkerInfoViewOffset() {
if (null != mHomeMapBaiduMapFragment) {
} else if (null != mHomeMapGoogleMapFragment) {
mHomeMapGoogleMapFragment!!.setMarkerInfoViewOffset()
}
}
// fun getAddressShowMarkerInfoWindow(historyDataBean: HistoryDataBean) {
// if (null != mHomeMapBaiduMapFragment) {
// mHomeMapBaiduMapFragment!!.showMarkerInfoWindow(historyDataBean)
// } else if (null != mHomeMapGoogleMapFragment) {
// mHomeMapGoogleMapFragment!!.showMarkerInfoWindow(historyDataBean)
// }
// }
}

View File

@@ -180,15 +180,7 @@ class RouteV2Fragment : BaseFragment<FragmentRouteV2Binding>(FragmentRouteV2Bind
} }
override fun onStopChanged(progress: Int) { override fun onStopChanged(progress: Int) {
// val historyDataBean = mHistoryDataList[progress]
// val address = historyDataBean.address
mHistoryDataMapCommon.slideStopChanged(progress) mHistoryDataMapCommon.slideStopChanged(progress)
// if (TextUtils.isEmpty(address)) {
// mHistoryDataMapCommon.slideStopChanged(progress)
// } else {
// mHistoryDataMapCommon.showInfoWindow(historyDataBean)
// }
} }
}) })

View File

@@ -87,7 +87,7 @@ abstract class BaseGoogleMapFragment :
private var mGoogleMapZoom = 17f private var mGoogleMapZoom = 17f
//地图摄像机移动动画时间 毫秒 //地图摄像机移动动画时间 毫秒
private val mAnimMoveDurationMs = 1000 private val mAnimMoveDurationMs = 800
//自定显示宠物头像地点信息图层 //自定显示宠物头像地点信息图层
var mMarker: Marker? = null var mMarker: Marker? = null

View File

@@ -92,6 +92,10 @@ class HistoryDataGoogleMapFragment : BaseGoogleMapFragment() {
//点击地图任意地方都不消失自定义头部地点布局 //点击地图任意地方都不消失自定义头部地点布局
googleMap.apply { googleMap.apply {
setOnCameraMoveListener { setOnCameraMoveListener {
//监听地图放大缩小操作
if (getGoogleMapZoom() != cameraPosition.zoom) {
setGoogleMapZoom(cameraPosition.zoom)
}
mCurLatLng?.let { mCurLatLng?.let {
if (mMarkerInfoView.isVisible) setMarkerInfoViewOffset(mMarkerInfoView, it) if (mMarkerInfoView.isVisible) setMarkerInfoViewOffset(mMarkerInfoView, it)
} }
@@ -195,9 +199,9 @@ class HistoryDataGoogleMapFragment : BaseGoogleMapFragment() {
*/ */
fun slideStopChanged(progress: Int) { fun slideStopChanged(progress: Int) {
if (mLatLngList.size > 0) { if (mLatLngList.size > 0) {
// moveCameraLocation( moveCameraLocation(
// mLatLngList[progress].latitude, mLatLngList[progress].longitude, false mLatLngList[progress].latitude, mLatLngList[progress].longitude, true
// ) )
mGeoCoderViewModel.getLatLonAddress( mGeoCoderViewModel.getLatLonAddress(
mLatLngList[progress].latitude, mLatLngList[progress].longitude, false mLatLngList[progress].latitude, mLatLngList[progress].longitude, false
) )

View File

@@ -1,313 +0,0 @@
package com.abbidot.tracker.ui.fragment.map.googlemap
import android.animation.ValueAnimator
import android.content.Context
import android.view.animation.LinearInterpolator
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.baselibrary.util.Utils
import com.abbidot.tracker.R
import com.abbidot.tracker.bean.HistoryDataBean
import com.abbidot.tracker.bean.MapDeviceBean
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.vm.GeoCoderViewModel
import com.abbidot.tracker.widget.MapDeviceNetView
import com.abbidot.tracker.widget.MapMarkerInfoView
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.model.Circle
import com.google.android.gms.maps.model.CircleOptions
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.Polyline
import com.google.android.gms.maps.model.PolylineOptions
import kotlin.math.pow
/**
*Created by .yzq on 2022/1/13/013.
* @link
* @description:首页map谷歌地图
*/
class HomeMapGoogleMapFragment : BaseGoogleMapFragment() {
private val mGeoCoderViewModel: GeoCoderViewModel by viewModels()
private lateinit var mMarkerInfoView: MapMarkerInfoView
private lateinit var mMapDeviceNetView: MapDeviceNetView
//宠物当前反地理位置信息
var mDecPetAddressData: HistoryDataBean? = null
private var mMapDeviceBean: MapDeviceBean? = null
private lateinit var mMapLoadOk: () -> Unit
//启动动画移动地图摄像机
private var isMoveCamera = true
private var mPolyline: Polyline? = null
private var mRippleCircle: Circle? = null
private var mValueAnimator: ValueAnimator? = null
// private var mAnimatorDuration=2000L
private var mRippleCirclePercent = 0.0
private var mRippleCircleRadius = 200
companion object {
@JvmStatic
fun newInstance(
context: Context,
markerInfoView: MapMarkerInfoView,
mapDeviceNetView: MapDeviceNetView,
mapLoadOk: () -> Unit
) = HomeMapGoogleMapFragment().apply {
mContext = context
mMarkerInfoView = markerInfoView
mMapDeviceNetView = mapDeviceNetView
mMapLoadOk = mapLoadOk
}
}
fun setMapDeviceBean(mapDeviceBean: MapDeviceBean?) {
mMapDeviceBean = mapDeviceBean
}
override fun liveDataObserve() {
//反地理编码成功返回
mGeoCoderViewModel.mLatLonAddressLiveData.observe(viewLifecycleOwner) {
//保存地理信息,方便传参给导航页面
if (null == mDecPetAddressData) mDecPetAddressData = HistoryDataBean()
mMapDeviceBean?.apply {
// val timeString = String.format(
// getString(R.string.map_current_update_time),
// DateUtils.getRelativeTimeSpanString(
// updateTime * 1000
// )
// )
val timeString =
Utils.formatTime(latLonUpdateTime * 1000, Utils.DATE_FORMAT_PATTERN_EN6)
mDecPetAddressData?.let { history ->
//更新位置时间
history.dayTime = timeString
//详细位置
history.address = it
//传递位置信息给InfoWindowAdapter窗口
// mMarker?.tag = history
// showMarkerInfoWindow(history)
ViewUtil.instance.viewShow(mMarkerInfoView)
mMarkerInfoView.setShowText(timeString, it)
}
}
}
}
override fun onMapLoadOk(googleMap: GoogleMap) {
// initFenceList()
//设置自定义头部地点布局
mGoogleMap?.apply {
setOnCameraMoveListener {
if (mMarkerInfoView.isVisible) setMarkerInfoViewOffset()
}
getLastLocation()
}
//变量是否初始化
if (::mMapLoadOk.isInitialized) mMapLoadOk()
}
override fun onRefreshPetCurrentLocationOk(googleMap: GoogleMap) {
if (null == mMarker) return
else startReverseGeocode()
}
/**
* 刚开始的反编译地理位置
*/
private fun startReverseGeocode() {
mMapDeviceBean?.apply {
mGeoCoderViewModel.getLatLonAddress(latitude, longitude, false)
setMarkerInfoViewOffset()
// val time = System.currentTimeMillis() - mReverseGeocodeTime
// //判断有没有超过设置的刷新时间
// if (time > mMapFragment.mMapDeviceBean!!.gnssInterval * 60 * 1000) {
// mGeoCoderViewModel.getLatLonAddress(mMapFragment.mMapDeviceBean!!.latitude,
// mMapFragment.mMapDeviceBean!!.longitude)
// } else {
// //传递位置信息给InfoWindowAdapter窗口
// mMarker?.tag = mMapFragment.mHistoryDataBean
// showMarkerInfoWindow()
// }
// }
}
}
fun setMarkerInfoViewOffset() {
mPetLatLng?.apply {
setMarkerInfoViewOffset(mMarkerInfoView, LatLng(latitude, longitude))
setMapNetTypeViewOffset(mMapDeviceNetView, LatLng(latitude, longitude))
}
}
/**
* 点击大头针开始更新反地理编码
*/
private fun reverseGeocode() {
// val time = System.currentTimeMillis() - mReverseGeocodeTime
//判断有没有超过设置的刷新时间
// if (time > mMapFragment.mMapDeviceBean!!.gnssInterval * 60 * 1000) {
// mGeoCoderViewModel.getLatLonAddress(mMapFragment.mMapDeviceBean!!.latitude,
// mMapFragment.mMapDeviceBean!!.longitude)
// }
}
/**
* 计算用户和宠物的距离
*/
fun getUserAndPetDistance(): Double {
var distance = 0.0
mUserLatLng?.apply {
mPetLatLng?.let {
distance =
Util.measureLatLonDistance(latitude, longitude, it.latitude, it.longitude)
}
}
return distance
}
/**
* 画用户和宠物之间的虚线
*/
private fun addUserAndPetLine(petLatLng: LatLng) {
mUserLatLng?.apply {
mGoogleMap?.let {
if (null == mPolyline) {
val polylineOptions =
PolylineOptions().clickable(false).width(AppUtils.dpToPx(1.5f))
.color(ContextCompat.getColor(mContext!!, R.color.blue_color9))
.geodesic(false)
mPolyline = it.addPolyline(polylineOptions)
mPolyline?.pattern = getDashedPatternStyle(18f)
}
mPolyline?.let { pLine ->
val latList = mutableListOf<LatLng>()
latList.add(petLatLng)
latList.add(this)
pLine.points = latList
}
}
}
}
/**
* 初始化并添加水波纹圆
*/
private fun addRippleCircle() {
mPetLatLng?.apply {
mGoogleMap?.let {
if (null == mRippleCircle) {
mRippleCircle = it.addCircle(
CircleOptions().center(this)
.radius(mRippleCircleRadius * mRippleCirclePercent)
.strokeWidth(AppUtils.dpToPx(2).toFloat())
.fillColor(ContextCompat.getColor(mContext!!, R.color.black10))
.strokeColor(ContextCompat.getColor(mContext!!, R.color.black10))
)
setRippleCircleAnim()
}
}
}
}
/**
* 设置水波纹动画
*/
private fun setRippleCircleAnim() {
if (null == mValueAnimator) {
mValueAnimator = ViewUtil.instance.setValueAnimator(
0.0, 100.0, 1500, LinearInterpolator()
) {
mRippleCirclePercent = it.animatedValue as Float / 100.0
mRippleCircle?.apply {
mPetLatLng?.let { petLatLng ->
center = petLatLng
radius = mRippleCircleRadius * mRippleCirclePercent
}
}
}?.apply {
repeatMode = ValueAnimator.RESTART
repeatCount = ValueAnimator.INFINITE
}
}
}
/**
* 画线
*/
fun addLine(latLng: LatLng) {
// addPolyline(mGoogleMap!!, latLng)
// addUserAndPetLine(latLng)
refreshPetCurrentLocation(latLng, needMoveCamera = isMoveCamera)
// mRippleCircle?.apply {
// //一像素对应几米
// val pxDis = 2.0.pow(15.5 - getGoogleMapZoom())
// mRippleCircleRadius = (pxDis * 150).toInt()
// fillColor = (ContextCompat.getColor(mContext!!, R.color.select_color5))
// strokeColor = (ContextCompat.getColor(mContext!!, R.color.select_color5))
// mValueAnimator?.duration = 2000
// }
}
/**
* 画多条线
*/
fun addLines(trackList: MutableList<HistoryDataBean>) {
val latLngList = mutableListOf<LatLng>()
for (i in 0 until trackList.size) {
trackList[i].apply {
val latLng = LatLng(latitude, longitude)
latLngList.add(latLng)
if (i == trackList.size - 1) {
// addUserAndPetLine(latLng)
refreshPetCurrentLocation(latLng, needMoveCamera = isMoveCamera)
}
}
}
// addPolyLines(mGoogleMap!!, latLngList)
}
/**
* 开始水波纹动画
*/
fun startRippleCircleAnim() {
mPetLatLng?.apply {
//一像素对应几米
val pxDis = 2.0.pow(15.5 - getGoogleMapZoom())
mRippleCircleRadius = (pxDis * 150).toInt()
// addUserAndPetLine(this)
addRippleCircle()
}
}
/**
* 直播开始之后,改变绿色波纹动画
*/
fun greenRippleCircleAnim() {
mRippleCircle?.apply {
//一像素对应几米
val pxDis = 2.0.pow(15.5 - getGoogleMapZoom())
mRippleCircleRadius = (pxDis * 150).toInt()
fillColor = (ContextCompat.getColor(mContext!!, R.color.select_color5))
strokeColor = (ContextCompat.getColor(mContext!!, R.color.select_color5))
mValueAnimator?.duration = 2000
}
}
override fun onDetach() {
super.onDetach()
mValueAnimator?.cancel()
}
}