Display the user's location
Display the user's location on a map with the default user location puck.
package com.mapbox.maps.testapp.examples import android.os.Bundleimport android.widget.Toastimport androidx.appcompat.app.AppCompatActivityimport com.mapbox.android.gestures.MoveGestureDetectorimport com.mapbox.maps.CameraOptionsimport com.mapbox.maps.ImageHolderimport com.mapbox.maps.MapViewimport com.mapbox.maps.Styleimport com.mapbox.maps.extension.style.expressions.dsl.generated.interpolateimport com.mapbox.maps.plugin.LocationPuck2Dimport com.mapbox.maps.plugin.PuckBearingimport com.mapbox.maps.plugin.gestures.OnMoveListenerimport com.mapbox.maps.plugin.gestures.gesturesimport com.mapbox.maps.plugin.locationcomponent.OnIndicatorBearingChangedListenerimport com.mapbox.maps.plugin.locationcomponent.OnIndicatorPositionChangedListenerimport com.mapbox.maps.plugin.locationcomponent.locationimport com.mapbox.maps.testapp.Rimport com.mapbox.maps.testapp.utils.LocationPermissionHelperimport java.lang.ref.WeakReference /*** Tracks the user location on screen, simulates a navigation session.*/class LocationTrackingActivity : AppCompatActivity() { private lateinit var locationPermissionHelper: LocationPermissionHelper private val onIndicatorBearingChangedListener = OnIndicatorBearingChangedListener {mapView.mapboxMap.setCamera(CameraOptions.Builder().bearing(it).build())} private val onIndicatorPositionChangedListener = OnIndicatorPositionChangedListener {mapView.mapboxMap.setCamera(CameraOptions.Builder().center(it).build())mapView.gestures.focalPoint = mapView.mapboxMap.pixelForCoordinate(it)} private val onMoveListener = object : OnMoveListener {override fun onMoveBegin(detector: MoveGestureDetector) {onCameraTrackingDismissed()} override fun onMove(detector: MoveGestureDetector): Boolean {return false} override fun onMoveEnd(detector: MoveGestureDetector) {}}private lateinit var mapView: MapView override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)mapView = MapView(this)setContentView(mapView)locationPermissionHelper = LocationPermissionHelper(WeakReference(this))locationPermissionHelper.checkPermissions {onMapReady()}} private fun onMapReady() {mapView.mapboxMap.setCamera(CameraOptions.Builder().zoom(14.0).build())mapView.mapboxMap.loadStyle(Style.STANDARD) {initLocationComponent()setupGesturesListener()}} private fun setupGesturesListener() {mapView.gestures.addOnMoveListener(onMoveListener)} private fun initLocationComponent() {val locationComponentPlugin = mapView.locationlocationComponentPlugin.updateSettings {puckBearing = PuckBearing.COURSEpuckBearingEnabled = trueenabled = truelocationPuck = LocationPuck2D(bearingImage = ImageHolder.from(R.drawable.mapbox_user_puck_icon),shadowImage = ImageHolder.from(R.drawable.mapbox_user_icon_shadow),scaleExpression = interpolate {linear()zoom()stop {literal(0.0)literal(0.6)}stop {literal(20.0)literal(1.0)}}.toJson())}locationComponentPlugin.addOnIndicatorPositionChangedListener(onIndicatorPositionChangedListener)locationComponentPlugin.addOnIndicatorBearingChangedListener(onIndicatorBearingChangedListener)} private fun onCameraTrackingDismissed() {Toast.makeText(this, "onCameraTrackingDismissed", Toast.LENGTH_SHORT).show()mapView.location.removeOnIndicatorPositionChangedListener(onIndicatorPositionChangedListener)mapView.location.removeOnIndicatorBearingChangedListener(onIndicatorBearingChangedListener)mapView.gestures.removeOnMoveListener(onMoveListener)} override fun onDestroy() {super.onDestroy()mapView.location.removeOnIndicatorBearingChangedListener(onIndicatorBearingChangedListener)mapView.location.removeOnIndicatorPositionChangedListener(onIndicatorPositionChangedListener)mapView.gestures.removeOnMoveListener(onMoveListener)} override fun onRequestPermissionsResult(requestCode: Int,permissions: Array<String>,grantResults: IntArray) {super.onRequestPermissionsResult(requestCode, permissions, grantResults)locationPermissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults)}}
Was this example helpful?