当 map.isMyLocationEnabled = false 时,FusedLocations 不会发送位置更新。奇怪的行为
Posted
技术标签:
【中文标题】当 map.isMyLocationEnabled = false 时,FusedLocations 不会发送位置更新。奇怪的行为【英文标题】:FusedLocations doesn't send location updates when map.isMyLocationEnabled = false. Strange behaviour 【发布时间】:2020-07-16 20:50:36 【问题描述】:我想添加一个自定义图像作为我的位置指示器。 隐藏蓝点的唯一方法是 setIsMyLocationEnabled = false。这样做也会以某种方式禁用来自 FusedLocation 的更新。我想知道这是有意为之还是只是一个错误?
Gradle -
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
活动(删除不必要的代码)-
class MapsActivity : AppCompatActivity(), OnMapReadyCallback
private lateinit var mMap: GoogleMap
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
startLocationUpdate()
mapFragment.getMapAsync(this)
private var marker: Marker? = null
private var lastLocation: Location? = null
override fun onMapReady(googleMap: GoogleMap)
mMap = googleMap
val sydney = LatLng(-34.0, 151.0)
val icon = BitmapDescriptorFactory.fromResource(R.drawable.ic_location_indicator)
val location = lastLocation?.let LatLng(it.latitude, it.longitude) ?: sydney
val markerOpts = MarkerOptions().position(location).flat(true).anchor(0.5f, 0.5f)
.icon(icon)
marker = mMap.addMarker(markerOpts)
val cameraPosition = CameraPosition.builder().zoom(17f).target(location).build()
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))
mMap.isMyLocationEnabled = false // disabling MyLocation here stops FusedLocation updates
mMap.uiSettings.isMyLocationButtonEnabled = true
fun startLocationUpdate()
val fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
fusedLocationClient.lastLocation
.addOnSuccessListener location: Location? ->
lastLocation = location
val locationRequest = LocationRequest()
locationRequest.fastestInterval = 1000
locationRequest.smallestDisplacement = 0f
fusedLocationClient.requestLocationUpdates(locationRequest, object :
LocationCallback()
override fun onLocationResult(p0: LocationResult?)
// Never gets called when mMap.isMyLocationEnabled = false but works properly
// if mMap.isMyLocationEnabled = true.
marker?.position = LatLng(p0.lastLocation.latitude, p0.lastLocation.longitude)
val cameraPosition = CameraPosition.builder().target(latlng).zoom(17f).build()
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))
, Looper.getMainLooper())
布局-
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_
android:layout_
tools:context=".ui.temp.MapsActivity" />
【问题讨论】:
【参考方案1】:问题出在位置请求上。默认情况下,LocationRequest 的优先级较低,因此不会经常更新。
当设置mMap.isMyLocationEnabled = false
时,它采用默认的低优先级。只需添加 2 行即可解决问题。
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
locationRequest.interval = 5000
【讨论】:
以上是关于当 map.isMyLocationEnabled = false 时,FusedLocations 不会发送位置更新。奇怪的行为的主要内容,如果未能解决你的问题,请参考以下文章