Location component
Shows location puck on the map.
<?xml version="1.0" encoding="utf-8"?><com.mapbox.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"xmlns:mapbox="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/mapView"android:layout_width="match_parent"android:layout_height="match_parent"mapbox:mapbox_locationComponentEnabled = "true"mapbox:mapbox_locationComponentPuckBearingSource = "heading"tools:context=".examples.LocationComponentActivity" />
package com.mapbox.maps.testapp.examples import android.os.Bundleimport android.view.Menuimport android.view.MenuItemimport android.widget.Toastimport androidx.appcompat.app.AppCompatActivityimport androidx.appcompat.content.res.AppCompatResourcesimport com.mapbox.maps.CameraOptionsimport com.mapbox.maps.Styleimport com.mapbox.maps.extension.style.expressions.dsl.generated.interpolateimport com.mapbox.maps.extension.style.layers.properties.generated.ProjectionNameimport com.mapbox.maps.extension.style.projection.generated.getProjectionimport com.mapbox.maps.extension.style.projection.generated.projectionimport com.mapbox.maps.extension.style.projection.generated.setProjectionimport com.mapbox.maps.plugin.LocationPuck2Dimport com.mapbox.maps.plugin.LocationPuck3Dimport com.mapbox.maps.plugin.PuckBearingSourceimport com.mapbox.maps.plugin.gestures.gesturesimport com.mapbox.maps.plugin.locationcomponent.DefaultLocationProviderimport com.mapbox.maps.plugin.locationcomponent.OnIndicatorPositionChangedListenerimport com.mapbox.maps.plugin.locationcomponent.createDefault2DPuckimport com.mapbox.maps.plugin.locationcomponent.locationimport com.mapbox.maps.plugin.locationcomponent.location2import com.mapbox.maps.testapp.Rimport com.mapbox.maps.testapp.databinding.ActivityLocationComponentBindingimport com.mapbox.maps.testapp.utils.LocationPermissionHelperimport java.lang.ref.WeakReference class LocationComponentActivity : AppCompatActivity() { private var lastStyleUri = Style.DARKprivate lateinit var locationPermissionHelper: LocationPermissionHelperprivate val onIndicatorPositionChangedListener = OnIndicatorPositionChangedListener {// Jump to the current indicator positionbinding.mapView.getMapboxMap().setCamera(CameraOptions.Builder().center(it).build())// Set the gestures plugin's focal point to the current indicator location.binding.mapView.gestures.focalPoint = binding.mapView.getMapboxMap().pixelForCoordinate(it)}private lateinit var binding: ActivityLocationComponentBinding override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)binding = ActivityLocationComponentBinding.inflate(layoutInflater)setContentView(binding.root)locationPermissionHelper = LocationPermissionHelper(WeakReference(this))locationPermissionHelper.checkPermissions {binding.mapView.apply {getMapboxMap().loadStyleUri(Style.MAPBOX_STREETS) {// Disable scroll gesture, since we are updating the camera position based on the indicator location.gestures.scrollEnabled = falsegestures.addOnMapClickListener { point ->location.isLocatedAt(point) { isPuckLocatedAtPoint ->if (isPuckLocatedAtPoint) {Toast.makeText(context, "Clicked on location puck", Toast.LENGTH_SHORT).show()}}true}gestures.addOnMapLongClickListener { point ->location.isLocatedAt(point) { isPuckLocatedAtPoint ->if (isPuckLocatedAtPoint) {Toast.makeText(context, "Long-clicked on location puck", Toast.LENGTH_SHORT).show()}}true}val locationProvider = location.getLocationProvider() as DefaultLocationProviderlocationProvider.addOnCompassCalibrationListener {Toast.makeText(context, "Compass needs to be calibrated", Toast.LENGTH_LONG).show()}}}}} override fun onCreateOptionsMenu(menu: Menu): Boolean {menuInflater.inflate(R.menu.menu_location_component, menu)return true} override fun onOptionsItemSelected(item: MenuItem): Boolean {when (item.itemId) {R.id.action_customise_location_puck_change -> {toggleCustomisedPuck()return true}R.id.action_map_style_change -> {toggleMapStyle()return true}R.id.action_map_projection_change -> {toggleMapProjection()return true}R.id.action_component_disable -> {binding.mapView.location.enabled = falsereturn true}R.id.action_component_enabled -> {binding.mapView.location.enabled = truereturn true}R.id.action_show_bearing -> {if (binding.mapView.location.locationPuck is LocationPuck2D) {binding.mapView.location.apply {locationPuck = createDefault2DPuck(this@LocationComponentActivity, withBearing = true)}}return true}R.id.action_hide_bearing -> {if (binding.mapView.location.locationPuck is LocationPuck2D) {binding.mapView.location.apply {locationPuck = createDefault2DPuck(this@LocationComponentActivity)}}return true}R.id.heading -> {binding.mapView.location2.puckBearingSource = PuckBearingSource.HEADINGitem.isChecked = truereturn true}R.id.course -> {binding.mapView.location2.puckBearingSource = PuckBearingSource.COURSEitem.isChecked = truereturn true}R.id.action_accuracy_enabled -> {binding.mapView.location2.showAccuracyRing = trueitem.isChecked = truereturn true}R.id.action_accuracy_disable -> {binding.mapView.location2.showAccuracyRing = falseitem.isChecked = truereturn true}R.id.toggle_opacity -> {val location = binding.mapView.locationlocation.locationPuck = location.locationPuck.run {when (this) {is LocationPuck3D -> copy(modelOpacity = if (modelOpacity == 1.0F) 0.5F else 1.0F)is LocationPuck2D -> copy(opacity = if (opacity == 1.0F) 0.5F else 1.0F)}}return true}else -> return super.onOptionsItemSelected(item)}} private fun toggleCustomisedPuck() {binding.mapView.location.let {when (it.locationPuck) {is LocationPuck3D -> it.locationPuck = LocationPuck2D(topImage = AppCompatResources.getDrawable(this,com.mapbox.maps.plugin.locationcomponent.R.drawable.mapbox_user_icon),bearingImage = AppCompatResources.getDrawable(this,com.mapbox.maps.plugin.locationcomponent.R.drawable.mapbox_user_bearing_icon),shadowImage = AppCompatResources.getDrawable(this,com.mapbox.maps.plugin.locationcomponent.R.drawable.mapbox_user_stroke_icon),scaleExpression = interpolate {linear()zoom()stop {literal(0.0)literal(0.6)}stop {literal(20.0)literal(1.0)}}.toJson())is LocationPuck2D -> it.locationPuck = LocationPuck3D(modelUri = "asset://sportcar.glb",modelScale = listOf(0.1f, 0.1f, 0.1f),modelTranslation = listOf(0.1f, 0.1f, 0.1f),modelRotation = listOf(0.0f, 0.0f, 180.0f),)}}} private fun toggleMapStyle() {val styleUrl = if (lastStyleUri == Style.DARK) Style.LIGHT else Style.DARKbinding.mapView.getMapboxMap().loadStyleUri(styleUrl) {lastStyleUri = styleUrl}} private fun toggleMapProjection() {binding.mapView.getMapboxMap().getStyle { style ->style.setProjection(projection(when (style.getProjection().name) {ProjectionName.MERCATOR -> ProjectionName.GLOBEProjectionName.GLOBE -> ProjectionName.MERCATOR}))}} override fun onRequestPermissionsResult(requestCode: Int,permissions: Array<String>,grantResults: IntArray) {super.onRequestPermissionsResult(requestCode, permissions, grantResults)locationPermissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults)} override fun onStart() {super.onStart()binding.mapView.location.addOnIndicatorPositionChangedListener(onIndicatorPositionChangedListener)} override fun onStop() {super.onStop()binding.mapView.location.removeOnIndicatorPositionChangedListener(onIndicatorPositionChangedListener)}}