自定义用户位置图标

This commit is contained in:
yezhiqiu
2026-03-11 16:00:08 +08:00
parent 1bc92e860f
commit 5e5679672f
2 changed files with 38 additions and 22 deletions

View File

@@ -1,9 +1,7 @@
package com.abbidot.tracker.ui.fragment.map.googlemap package com.abbidot.tracker.ui.fragment.map.googlemap
import android.Manifest
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.content.pm.PackageManager
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.Point import android.graphics.Point
import android.location.Location import android.location.Location
@@ -11,7 +9,6 @@ import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.abbidot.baselibrary.constant.MMKVKey import com.abbidot.baselibrary.constant.MMKVKey
@@ -93,6 +90,9 @@ abstract class BaseGoogleMapFragment :
//自定显示宠物头像地点信息图层 //自定显示宠物头像地点信息图层
var mMarker: Marker? = null var mMarker: Marker? = null
//自定显示用户地点信息图层
var mUserMarker: Marker? = null
//宠物大头针设置自定义头像 //宠物大头针设置自定义头像
private var mPetHeadIconBitmap: Bitmap? = null private var mPetHeadIconBitmap: Bitmap? = null
@@ -300,18 +300,15 @@ abstract class BaseGoogleMapFragment :
* 切换用户和宠物的位置居中显示,移动摄像头中心 * 切换用户和宠物的位置居中显示,移动摄像头中心
*/ */
fun switchShowLocation(@ConstantInt type: Int) { fun switchShowLocation(@ConstantInt type: Int) {
if (type == ConstantInt.PetLocationType) { val latLng = if (type == ConstantInt.PetLocationType) {
//刷新下用户当前的位置 //刷新下用户当前的位置
getLastLocation() getLastLocation()
mPetLatLng?.let { mPetLatLng
moveCameraLocation(it)
}
} else { } else {
mGoogleMap?.apply { mUserLatLng
mUserLatLng?.let { }
moveCamera(CameraUpdateFactory.newLatLngZoom(it, mGoogleMapZoom)) latLng?.let {
} moveCameraLocation(it)
}
} }
} }
@@ -372,6 +369,21 @@ abstract class BaseGoogleMapFragment :
mGoogleMap?.apply { mGoogleMap?.apply {
mMarker = addMarker(markerOptions) mMarker = addMarker(markerOptions)
} }
setUserMarker()
}
fun setUserMarker() {
mUserLatLng?.let {
if (null == mUserMarker) {
mUserMarker = addImageMarker(it, R.drawable.pic_avatar_df)
} else {
if (it.latitude == mUserMarker!!.position.latitude && it.longitude == mUserMarker!!.position.longitude) {
} else {
mUserMarker?.remove()
mUserMarker = addImageMarker(it, R.drawable.pic_avatar_df)
}
}
}
} }
/** /**
@@ -902,15 +914,15 @@ abstract class BaseGoogleMapFragment :
//显示实时交通信息 //显示实时交通信息
// isTrafficEnabled = true // isTrafficEnabled = true
//检测权限开启用户的当前位置https://developers.google.cn/maps/documentation/android-sdk/location?hl=zh-cn //检测权限开启用户的当前位置https://developers.google.cn/maps/documentation/android-sdk/location?hl=zh-cn
if (ActivityCompat.checkSelfPermission( // if (ActivityCompat.checkSelfPermission(
mContext!!, Manifest.permission.ACCESS_FINE_LOCATION // mContext!!, Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( // ) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
mContext!!, Manifest.permission.ACCESS_COARSE_LOCATION // mContext!!, Manifest.permission.ACCESS_COARSE_LOCATION
) == PackageManager.PERMISSION_GRANTED // ) == PackageManager.PERMISSION_GRANTED
) { // ) {
//启用“我的位置”图层。 // //启用“我的位置”图层。
isMyLocationEnabled = true // isMyLocationEnabled = true
} // }
uiSettings.let { uiSettings.let {
//禁止显示“我的位置”按钮。 //禁止显示“我的位置”按钮。
it.isMyLocationButtonEnabled = false it.isMyLocationButtonEnabled = false
@@ -952,7 +964,10 @@ abstract class BaseGoogleMapFragment :
} }
if (null != location) { if (null != location) {
mUserLatLng = LatLng(location.latitude, location.longitude) mUserLatLng = LatLng(location.latitude, location.longitude)
if (isLocationSuccessCallback) locationSuccess(location) if (isLocationSuccessCallback) {
setUserMarker()
locationSuccess(location)
}
isLocationSuccessCallback = false isLocationSuccessCallback = false
} else requestCurrentLocation() } else requestCurrentLocation()
} }

View File

@@ -297,6 +297,7 @@ class HistoryDataGoogleMapFragment : BaseGoogleMapFragment() {
mMarker = addMarker(mMarkerOptions) mMarker = addMarker(mMarkerOptions)
//显示最上面,层级最高 //显示最上面,层级最高
mMarker?.zIndex = 1f mMarker?.zIndex = 1f
setUserMarker()
} }
} }
} }