Skip to content

Commit 9191283

Browse files
committed
Add multiple location components example.
1 parent 00fd47a commit 9191283

7 files changed

Lines changed: 174 additions & 4 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@
11971197
</activity>
11981198

11991199
<activity
1200-
android:name=".examples.CustomJourneyLocationProviderActivity"
1200+
android:name=".examples.location.CustomJourneyLocationProviderActivity"
12011201
android:description="@string/description_custom_location_provider"
12021202
android:exported="true"
12031203
android:label="@string/activity_custom_location_provider">
@@ -1209,6 +1209,19 @@
12091209
android:value=".ExampleOverviewActivity" />
12101210
</activity>
12111211

1212+
<activity
1213+
android:name=".examples.location.MultipleLocationComponentActivity"
1214+
android:description="@string/description_multiple_location_component"
1215+
android:exported="true"
1216+
android:label="@string/activity_multiple_location_component">
1217+
<meta-data
1218+
android:name="@string/category"
1219+
android:value="@string/category_location" />
1220+
<meta-data
1221+
android:name="android.support.PARENT_ACTIVITY"
1222+
android:value=".ExampleOverviewActivity" />
1223+
</activity>
1224+
12121225
<activity
12131226
android:name=".TestMapActivity"
12141227
android:exported="true" />

app/src/main/java/com/mapbox/maps/testapp/examples/CustomJourneyLocationProviderActivity.kt renamed to app/src/main/java/com/mapbox/maps/testapp/examples/location/CustomJourneyLocationProviderActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.mapbox.maps.testapp.examples
1+
package com.mapbox.maps.testapp.examples.location
22

33
import android.annotation.SuppressLint
44
import android.os.Bundle
@@ -69,7 +69,7 @@ class CustomJourneyLocationProviderActivity : AppCompatActivity(), OnMapClickLis
6969
locationComponentPlugin2.let {
7070
it.locationPuck = LocationPuck3D(
7171
modelUri = "asset://sportcar.glb",
72-
modelScale = listOf(0.5f, 0.5f, 0.5f),
72+
modelScale = listOf(0.2f, 0.2f, 0.2f),
7373
modelTranslation = listOf(0.1f, 0.1f, 0.1f),
7474
modelRotation = listOf(0.0f, 0.0f, 180.0f)
7575
)
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.mapbox.maps.testapp.examples.location
2+
3+
import android.annotation.SuppressLint
4+
import android.os.Bundle
5+
import android.view.ViewGroup
6+
import android.widget.Button
7+
import androidx.appcompat.app.AppCompatActivity
8+
import com.mapbox.geojson.Point
9+
import com.mapbox.maps.CameraOptions
10+
import com.mapbox.maps.MapView
11+
import com.mapbox.maps.Style
12+
import com.mapbox.maps.extension.style.expressions.dsl.generated.literal
13+
import com.mapbox.maps.plugin.LocationPuck2D
14+
import com.mapbox.maps.plugin.LocationPuck3D
15+
import com.mapbox.maps.plugin.gestures.OnMapClickListener
16+
import com.mapbox.maps.plugin.gestures.gestures
17+
import com.mapbox.maps.plugin.locationcomponent.CustomJourneyLocationProvider
18+
import com.mapbox.maps.plugin.locationcomponent.Journey
19+
import com.mapbox.maps.plugin.locationcomponent.LocationComponentPlugin2
20+
import com.mapbox.maps.plugin.locationcomponent.location2
21+
import com.mapbox.maps.testapp.utils.LocationComponentUtils
22+
import com.mapbox.maps.testapp.utils.createLocationComponent
23+
import java.util.concurrent.CopyOnWriteArraySet
24+
25+
/**
26+
* Example of using multiple location component.
27+
*/
28+
class MultipleLocationComponentActivity : AppCompatActivity(), OnMapClickListener {
29+
30+
private val journey = Journey()
31+
private val customJourneyLocationProvider = CustomJourneyLocationProvider().apply { loadJourney(journey) }
32+
private lateinit var mapView: MapView
33+
private val locationComponents = CopyOnWriteArraySet<LocationComponentPlugin2>()
34+
35+
@SuppressLint("SetTextI18n")
36+
override fun onCreate(savedInstanceState: Bundle?) {
37+
super.onCreate(savedInstanceState)
38+
mapView = MapView(this)
39+
setContentView(mapView)
40+
mapView.addView(
41+
Button(this).apply {
42+
layoutParams = ViewGroup.LayoutParams(
43+
ViewGroup.LayoutParams.WRAP_CONTENT,
44+
ViewGroup.LayoutParams.WRAP_CONTENT
45+
)
46+
text = "Cancel"
47+
setOnClickListener {
48+
journey.pause()
49+
}
50+
}
51+
)
52+
mapView.getMapboxMap()
53+
.apply {
54+
setCamera(
55+
CameraOptions.Builder()
56+
.center(HELSINKI)
57+
.pitch(40.0)
58+
.zoom(14.0)
59+
.build()
60+
)
61+
loadStyleUri(Style.MAPBOX_STREETS) {
62+
initLocationComponents()
63+
initClickListeners()
64+
journey.start()
65+
}
66+
}
67+
}
68+
69+
private fun initClickListeners() {
70+
mapView.gestures.addOnMapClickListener(this)
71+
}
72+
73+
private fun initLocationComponents() {
74+
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+
}
85+
)
86+
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+
}
98+
)
99+
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+
}
111+
)
112+
113+
}
114+
115+
override fun onMapClick(point: Point): Boolean {
116+
journey.queueLocationUpdate(point)
117+
return true
118+
}
119+
120+
companion object {
121+
private val HELSINKI = Point.fromLngLat(24.9384, 60.1699)
122+
}
123+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.mapbox.maps.testapp.utils
2+
3+
import com.mapbox.maps.MapView
4+
import com.mapbox.maps.plugin.Plugin
5+
import com.mapbox.maps.plugin.locationcomponent.LocationComponentInitOptions
6+
import com.mapbox.maps.plugin.locationcomponent.LocationComponentPlugin2
7+
import com.mapbox.maps.plugin.locationcomponent.LocationComponentPluginImpl
8+
9+
public fun MapView.createLocationComponent(locationComponentInitOptions: LocationComponentInitOptions): LocationComponentPlugin2 {
10+
val locationComponent = LocationComponentPluginImpl(locationComponentInitOptions)
11+
val locationComponentPlugin = Plugin.Custom(
12+
locationComponentInitOptions.hashCode().toString(),
13+
locationComponent
14+
)
15+
this.createPlugin(locationComponentPlugin)
16+
return locationComponent
17+
}
18+
19+
object LocationComponentUtils {
20+
private var custom2DLayerIdCount = 0
21+
private var custom3DLayerIdCount = 0
22+
private var custom3DSourceIdCount = 0
23+
24+
fun getNextLocationComponentOptions() = LocationComponentInitOptions {
25+
puck2DLayerId = "custom_location_component_2d_layer_$custom2DLayerIdCount"
26+
puck3DLayerId = "custom_location_component_3d_layer_$custom3DLayerIdCount"
27+
puck3DSourceId = "custom_location_component_3d_source_$custom3DSourceIdCount"
28+
custom2DLayerIdCount++
29+
custom3DLayerIdCount++
30+
custom3DSourceIdCount++
31+
}
32+
}

app/src/main/res/values/example_descriptions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,5 @@
9595
<string name="description_advanced_viewport">Advanced viewport with gestures showcase</string>
9696
<string name="description_third_party_source">Add a third party vector tile source.</string>
9797
<string name="description_custom_location_provider">Showcase usage of custom location provider.</string>
98+
<string name="description_multiple_location_component">Showcase usage of multiple location component.</string>
9899
</resources>

app/src/main/res/values/example_titles.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,5 @@
9595
<string name="activity_advanced_viewport">Advanced Viewport with gestures</string>
9696
<string name="activity_third_party_source">External Vector Source</string>
9797
<string name="activity_custom_location_provider">Custom location provider</string>
98+
<string name="activity_multiple_location_component">Multiple location components</string>
9899
</resources>

plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/CustomJourneyLocationProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class CustomJourneyLocationProvider : LocationProvider {
5151
}
5252

5353
@MapboxExperimental
54-
class Journey(val speed: Double = 100.0, val angularSpeed: Double = 100.0) {
54+
class Journey(val speed: Double = 100.0, val angularSpeed: Double = 500.0) {
5555
private val locationList = CopyOnWriteArrayList<QueueData>()
5656
private val initialTimeStamp: Long = 0
5757
private val remainingPoints = ConcurrentLinkedQueue<QueueData>()

0 commit comments

Comments
 (0)