@@ -3,34 +3,46 @@ package com.mapbox.maps.testapp.examples.location
3
3
import android.annotation.SuppressLint
4
4
import android.os.Bundle
5
5
import android.view.ViewGroup
6
+ import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
6
7
import android.widget.Button
7
8
import androidx.appcompat.app.AppCompatActivity
8
9
import com.mapbox.geojson.Point
9
10
import com.mapbox.maps.CameraOptions
10
11
import com.mapbox.maps.MapView
12
+ import com.mapbox.maps.MapboxExperimental
11
13
import com.mapbox.maps.Style
12
- import com.mapbox.maps.extension.style.expressions.dsl.generated.literal
13
14
import com.mapbox.maps.plugin.LocationPuck2D
14
15
import com.mapbox.maps.plugin.LocationPuck3D
16
+ import com.mapbox.maps.plugin.annotation.annotations
17
+ import com.mapbox.maps.plugin.annotation.generated.*
15
18
import com.mapbox.maps.plugin.gestures.OnMapClickListener
16
19
import com.mapbox.maps.plugin.gestures.gestures
17
20
import com.mapbox.maps.plugin.locationcomponent.CustomJourneyLocationProvider
18
21
import com.mapbox.maps.plugin.locationcomponent.Journey
19
22
import 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
21
25
import com.mapbox.maps.testapp.utils.LocationComponentUtils
22
26
import com.mapbox.maps.testapp.utils.createLocationComponent
23
- import java.util.concurrent.CopyOnWriteArraySet
27
+ import java.util.*
24
28
25
29
/* *
26
30
* Example of using multiple location component.
27
31
*/
32
+ @OptIn(MapboxExperimental ::class )
28
33
class MultipleLocationComponentActivity : AppCompatActivity (), OnMapClickListener {
29
-
30
- private val journey = Journey ()
31
- private val customJourneyLocationProvider = CustomJourneyLocationProvider ().apply { loadJourney(journey) }
32
34
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
34
46
35
47
@SuppressLint(" SetTextI18n" )
36
48
override fun onCreate (savedInstanceState : Bundle ? ) {
@@ -39,16 +51,15 @@ class MultipleLocationComponentActivity : AppCompatActivity(), OnMapClickListene
39
51
setContentView(mapView)
40
52
mapView.addView(
41
53
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" } "
47
56
setOnClickListener {
48
- journey.pause()
57
+ toggleControl()
58
+ text = " Controlling ${if (selectedPuck == 0 ) " Car" else " Duck" } "
49
59
}
50
60
}
51
61
)
62
+ pointAnnotationManager = mapView.annotations.createPointAnnotationManager()
52
63
mapView.getMapboxMap()
53
64
.apply {
54
65
setCamera(
@@ -61,59 +72,100 @@ class MultipleLocationComponentActivity : AppCompatActivity(), OnMapClickListene
61
72
loadStyleUri(Style .MAPBOX_STREETS ) {
62
73
initLocationComponents()
63
74
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)
65
83
}
84
+ true
66
85
}
86
+ }
87
+ }
88
+
89
+ private fun toggleControl () {
90
+ selectedPuck = (selectedPuck + 1 ) % 2
67
91
}
68
92
69
93
private fun initClickListeners () {
70
94
mapView.gestures.addOnMapClickListener(this )
71
95
}
72
96
73
97
private fun initLocationComponents () {
98
+ // Puck with pulsing car
74
99
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
+ }
85
111
)
86
112
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
+ }
98
125
)
126
+ // Puck with pulsing duck
99
127
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
+ }
111
153
)
112
-
113
154
}
114
155
115
156
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
+ }
117
169
return true
118
170
}
119
171
0 commit comments