Mapbox: Implement option to display user location

Co-Authored-By: Vincent Vidal <vinvidal@gmail.com>
This commit is contained in:
Marvin W 2020-10-09 16:30:43 +02:00
parent 775b18c203
commit 981301e9c6
No known key found for this signature in database
GPG Key ID: 072E9235DB996F2A
1 changed files with 35 additions and 7 deletions

View File

@ -17,7 +17,6 @@
package org.microg.gms.maps.mapbox package org.microg.gms.maps.mapbox
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context import android.content.Context
import android.location.Location import android.location.Location
import android.os.* import android.os.*
@ -41,14 +40,16 @@ import com.mapbox.mapboxsdk.Mapbox
import com.mapbox.mapboxsdk.R import com.mapbox.mapboxsdk.R
import com.mapbox.mapboxsdk.camera.CameraUpdate import com.mapbox.mapboxsdk.camera.CameraUpdate
import com.mapbox.mapboxsdk.constants.MapboxConstants import com.mapbox.mapboxsdk.constants.MapboxConstants
import com.mapbox.mapboxsdk.location.LocationComponentActivationOptions
import com.mapbox.mapboxsdk.location.LocationComponentOptions
import com.mapbox.mapboxsdk.location.modes.CameraMode
import com.mapbox.mapboxsdk.location.modes.RenderMode
import com.mapbox.mapboxsdk.maps.MapView import com.mapbox.mapboxsdk.maps.MapView
import com.mapbox.mapboxsdk.maps.MapboxMap import com.mapbox.mapboxsdk.maps.MapboxMap
import com.mapbox.mapboxsdk.maps.Style import com.mapbox.mapboxsdk.maps.Style
import com.mapbox.mapboxsdk.plugins.annotation.* import com.mapbox.mapboxsdk.plugins.annotation.*
import com.mapbox.mapboxsdk.plugins.annotation.Annotation import com.mapbox.mapboxsdk.plugins.annotation.Annotation
import com.mapbox.mapboxsdk.style.layers.Property.LINE_CAP_ROUND import com.mapbox.mapboxsdk.style.layers.Property.LINE_CAP_ROUND
import com.mapbox.mapboxsdk.utils.ColorUtils
import com.mapbox.mapboxsdk.utils.ThreadUtils
import org.microg.gms.kotlin.unwrap import org.microg.gms.kotlin.unwrap
import org.microg.gms.maps.MapsConstants.* import org.microg.gms.maps.MapsConstants.*
import org.microg.gms.maps.mapbox.model.* import org.microg.gms.maps.mapbox.model.*
@ -117,6 +118,7 @@ class GoogleMapImpl(private val context: Context, var options: GoogleMapOptions)
var storedMapType: Int = options.mapType var storedMapType: Int = options.mapType
val waitingCameraUpdates = mutableListOf<CameraUpdate>() val waitingCameraUpdates = mutableListOf<CameraUpdate>()
var locationEnabled: Boolean = false
init { init {
val mapContext = MapContext(context) val mapContext = MapContext(context)
@ -388,13 +390,24 @@ class GoogleMapImpl(private val context: Context, var options: GoogleMapOptions)
} }
override fun isMyLocationEnabled(): Boolean { override fun isMyLocationEnabled(): Boolean {
Log.d(TAG, "unimplemented Method: isMyLocationEnabled") return locationEnabled
return false
} }
override fun setMyLocationEnabled(myLocation: Boolean) { override fun setMyLocationEnabled(myLocation: Boolean) {
Log.d(TAG, "unimplemented Method: setMyLocationEnabled") synchronized(mapLock) {
locationEnabled = myLocation
if (!loaded) return
val locationComponent = map?.locationComponent ?: return
try {
if (locationComponent.isLocationComponentActivated) {
locationComponent.isLocationComponentEnabled = myLocation
}
} catch (e: SecurityException) {
Log.w(TAG, e)
locationEnabled = false
}
Unit
}
} }
override fun getMyLocation(): Location? { override fun getMyLocation(): Location? {
@ -692,7 +705,22 @@ class GoogleMapImpl(private val context: Context, var options: GoogleMapOptions)
pendingMarkers.forEach { it.update(symbolManager) } pendingMarkers.forEach { it.update(symbolManager) }
pendingMarkers.clear() pendingMarkers.clear()
val mapContext = MapContext(context)
map.locationComponent.apply {
activateLocationComponent(LocationComponentActivationOptions.builder(mapContext, it)
.locationComponentOptions(LocationComponentOptions.builder(mapContext).pulseEnabled(true).build())
.build())
cameraMode = CameraMode.TRACKING
renderMode = RenderMode.COMPASS
}
synchronized(mapLock) { synchronized(mapLock) {
try {
map.locationComponent.isLocationComponentEnabled = locationEnabled
} catch (e: SecurityException) {
Log.w(TAG, e)
locationEnabled = false
}
loaded = true loaded = true
if (loadedCallback != null) { if (loadedCallback != null) {
Log.d(TAG, "Invoking callback delayed, as map is loaded") Log.d(TAG, "Invoking callback delayed, as map is loaded")