@@ -3,34 +3,46 @@ package com.mapbox.maps.testapp.examples.location
33import android.annotation.SuppressLint
44import android.os.Bundle
55import android.view.ViewGroup
6+ import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
67import android.widget.Button
78import androidx.appcompat.app.AppCompatActivity
89import com.mapbox.geojson.Point
910import com.mapbox.maps.CameraOptions
1011import com.mapbox.maps.MapView
12+ import com.mapbox.maps.MapboxExperimental
1113import com.mapbox.maps.Style
12- import com.mapbox.maps.extension.style.expressions.dsl.generated.literal
1314import com.mapbox.maps.plugin.LocationPuck2D
1415import com.mapbox.maps.plugin.LocationPuck3D
16+ import com.mapbox.maps.plugin.annotation.annotations
17+ import com.mapbox.maps.plugin.annotation.generated.*
1518import com.mapbox.maps.plugin.gestures.OnMapClickListener
1619import com.mapbox.maps.plugin.gestures.gestures
1720import com.mapbox.maps.plugin.locationcomponent.CustomJourneyLocationProvider
1821import com.mapbox.maps.plugin.locationcomponent.Journey
1922import com.mapbox.maps.plugin.locationcomponent.LocationComponentPlugin2
20- import com.mapbox.maps.plugin.locationcomponent.location2
23+ import com.mapbox.maps.testapp.R
24+ import com.mapbox.maps.testapp.utils.BitmapUtils
2125import com.mapbox.maps.testapp.utils.LocationComponentUtils
2226import com.mapbox.maps.testapp.utils.createLocationComponent
23- import java.util.concurrent.CopyOnWriteArraySet
27+ import java.util.*
2428
2529/* *
2630 * Example of using multiple location component.
2731 */
32+ @OptIn(MapboxExperimental ::class )
2833class MultipleLocationComponentActivity : AppCompatActivity (), OnMapClickListener {
29-
30- private val journey = Journey ()
31- private val customJourneyLocationProvider = CustomJourneyLocationProvider ().apply { loadJourney(journey) }
3234 private lateinit var mapView: MapView
33- private val locationComponents = CopyOnWriteArraySet <LocationComponentPlugin2 >()
35+ private val journeys = mutableListOf (Journey (speed = 150.0 ), Journey (speed = 100.0 ))
36+ private val customJourneyLocationProviders = mutableListOf (
37+ CustomJourneyLocationProvider ().apply { loadJourney(journeys.first()) },
38+ CustomJourneyLocationProvider ().apply { loadJourney(journeys.last()) }
39+ )
40+ private val annotationLists =
41+ mutableListOf (LinkedList <PointAnnotation >(), LinkedList <PointAnnotation >())
42+ private val locationComponents = mutableListOf<LocationComponentPlugin2 >()
43+ private lateinit var pointAnnotationManager: PointAnnotationManager
44+
45+ var selectedPuck = 0
3446
3547 @SuppressLint(" SetTextI18n" )
3648 override fun onCreate (savedInstanceState : Bundle ? ) {
@@ -39,16 +51,15 @@ class MultipleLocationComponentActivity : AppCompatActivity(), OnMapClickListene
3951 setContentView(mapView)
4052 mapView.addView(
4153 Button (this ).apply {
42- layoutParams = ViewGroup .LayoutParams (
43- ViewGroup .LayoutParams .WRAP_CONTENT ,
44- ViewGroup .LayoutParams .WRAP_CONTENT
45- )
46- text = " Cancel"
54+ layoutParams = ViewGroup .LayoutParams (WRAP_CONTENT , WRAP_CONTENT )
55+ text = " Controlling ${if (selectedPuck == 0 ) " Car" else " Duck" } "
4756 setOnClickListener {
48- journey.pause()
57+ toggleControl()
58+ text = " Controlling ${if (selectedPuck == 0 ) " Car" else " Duck" } "
4959 }
5060 }
5161 )
62+ pointAnnotationManager = mapView.annotations.createPointAnnotationManager()
5263 mapView.getMapboxMap()
5364 .apply {
5465 setCamera(
@@ -61,59 +72,100 @@ class MultipleLocationComponentActivity : AppCompatActivity(), OnMapClickListene
6172 loadStyleUri(Style .MAPBOX_STREETS ) {
6273 initLocationComponents()
6374 initClickListeners()
64- journey.start()
75+ journeys.forEach { it.start() }
76+ }
77+ }
78+
79+ journeys.forEachIndexed { index, journey ->
80+ journey.observeJourneyUpdates { _, _, _, _ ->
81+ annotationLists[index].poll()?.let {
82+ pointAnnotationManager.delete(it)
6583 }
84+ true
6685 }
86+ }
87+ }
88+
89+ private fun toggleControl () {
90+ selectedPuck = (selectedPuck + 1 ) % 2
6791 }
6892
6993 private fun initClickListeners () {
7094 mapView.gestures.addOnMapClickListener(this )
7195 }
7296
7397 private fun initLocationComponents () {
98+ // Puck with pulsing car
7499 locationComponents.add(
75- mapView.createLocationComponent(LocationComponentUtils .getNextLocationComponentOptions()).apply {
76- setLocationProvider(customJourneyLocationProvider)
77- enabled = true
78- locationPuck = LocationPuck2D (
79- topImage = null ,
80- bearingImage = null ,
81- )
82- puckBearingEnabled = true
83- pulsingEnabled = true
84- }
100+ mapView.createLocationComponent(LocationComponentUtils .getNextLocationComponentOptions())
101+ .apply {
102+ setLocationProvider(customJourneyLocationProviders.first())
103+ enabled = true
104+ locationPuck = LocationPuck2D (
105+ topImage = null ,
106+ bearingImage = null ,
107+ )
108+ puckBearingEnabled = true
109+ pulsingEnabled = true
110+ }
85111 )
86112 locationComponents.add(
87- mapView.createLocationComponent(LocationComponentUtils .getNextLocationComponentOptions()).apply {
88- setLocationProvider(customJourneyLocationProvider)
89- enabled = true
90- locationPuck = LocationPuck3D (
91- modelUri = " asset://sportcar.glb" ,
92- modelScale = listOf (0.2f , 0.2f , 0.2f ),
93- modelTranslation = listOf (0.1f , 0.1f , 0.1f ),
94- modelRotation = listOf (0.0f , 0.0f , 180.0f )
95- )
96- puckBearingEnabled = true
97- }
113+ mapView.createLocationComponent(LocationComponentUtils .getNextLocationComponentOptions())
114+ .apply {
115+ setLocationProvider(customJourneyLocationProviders.first())
116+ enabled = true
117+ locationPuck = LocationPuck3D (
118+ modelUri = " asset://sportcar.glb" ,
119+ modelScale = listOf (0.3f , 0.3f , 0.3f ),
120+ modelTranslation = listOf (0.1f , 0.1f , 0.1f ),
121+ modelRotation = listOf (0.0f , 0.0f , 180.0f )
122+ )
123+ puckBearingEnabled = true
124+ }
98125 )
126+ // Puck with pulsing duck
99127 locationComponents.add(
100- mapView.createLocationComponent(LocationComponentUtils .getNextLocationComponentOptions()).apply {
101- setLocationProvider(customJourneyLocationProvider)
102- enabled = true
103- locationPuck = LocationPuck3D (
104- modelUri = " https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Duck/glTF-Embedded/Duck.gltf" ,
105- modelScale = listOf (0.2f , 0.2f , 0.2f ),
106- modelRotation = listOf (0f , 0f , - 90f ),
107- modelTranslation = listOf (0f , 0.0f , 0.0f )
108- )
109- puckBearingEnabled = true
110- }
128+ mapView.createLocationComponent(LocationComponentUtils .getNextLocationComponentOptions())
129+ .apply {
130+ setLocationProvider(customJourneyLocationProviders.last())
131+ enabled = true
132+ locationPuck = LocationPuck2D (
133+ topImage = null ,
134+ bearingImage = null ,
135+ )
136+ puckBearingEnabled = true
137+ pulsingEnabled = true
138+ }
139+ )
140+ locationComponents.add(
141+ mapView.createLocationComponent(LocationComponentUtils .getNextLocationComponentOptions())
142+ .apply {
143+ setLocationProvider(customJourneyLocationProviders.last())
144+ enabled = true
145+ locationPuck = LocationPuck3D (
146+ modelUri = " https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Duck/glTF-Embedded/Duck.gltf" ,
147+ modelScale = listOf (0.5f , 0.5f , 0.5f ),
148+ modelRotation = listOf (0f , 0f , - 90f ),
149+ modelTranslation = listOf (0f , 0.0f , 0.0f )
150+ )
151+ puckBearingEnabled = true
152+ }
111153 )
112-
113154 }
114155
115156 override fun onMapClick (point : Point ): Boolean {
116- journey.queueLocationUpdate(point)
157+ journeys[selectedPuck].queueLocationUpdate(point)
158+ BitmapUtils .bitmapFromDrawableRes(
159+ this ,
160+ if (selectedPuck == 0 ) R .drawable.red_marker else R .drawable.blue_marker_view
161+ )?.let {
162+ pointAnnotationManager.create(
163+ PointAnnotationOptions ()
164+ .withPoint(point)
165+ .withIconImage(it)
166+ .withDraggable(true )
167+ ).also { annotation -> annotationLists[selectedPuck].add(annotation) }
168+ }
117169 return true
118170 }
119171
0 commit comments