Я хочу добавить пользовательское изображение в качестве индикатора моего местоположения. Единственный способ скрыть синюю точку - это 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())
}
}
Layout-
<?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_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.temp.MapsActivity" />