Skip to content

Commit 3dccf51

Browse files
committed
Add MultiPuckViewportState.
1 parent 827b26f commit 3dccf51

File tree

10 files changed

+615
-47
lines changed

10 files changed

+615
-47
lines changed

app/src/main/java/com/mapbox/maps/testapp/examples/location/MultipleLocationComponentActivity.kt

+36-20
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import android.os.Bundle
55
import android.view.ViewGroup
66
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
77
import android.widget.Button
8+
import android.widget.LinearLayout
89
import androidx.appcompat.app.AppCompatActivity
910
import com.mapbox.geojson.Point
10-
import com.mapbox.maps.CameraOptions
11-
import com.mapbox.maps.MapView
12-
import com.mapbox.maps.MapboxExperimental
13-
import com.mapbox.maps.Style
11+
import com.mapbox.maps.*
1412
import com.mapbox.maps.plugin.LocationPuck2D
1513
import com.mapbox.maps.plugin.LocationPuck3D
1614
import com.mapbox.maps.plugin.annotation.annotations
@@ -19,10 +17,18 @@ import com.mapbox.maps.plugin.gestures.OnMapClickListener
1917
import com.mapbox.maps.plugin.gestures.gestures
2018
import com.mapbox.maps.plugin.locationcomponent.CustomJourneyLocationProvider
2119
import com.mapbox.maps.plugin.locationcomponent.Journey
20+
import com.mapbox.maps.plugin.locationcomponent.LocationComponentInitOptions
2221
import com.mapbox.maps.plugin.locationcomponent.LocationComponentPlugin2
22+
import com.mapbox.maps.plugin.viewport.data.FollowPuckViewportStateBearing
23+
import com.mapbox.maps.plugin.viewport.data.FollowPuckViewportStateOptions
24+
import com.mapbox.maps.plugin.viewport.data.MultiPuckViewportStateBearing
25+
import com.mapbox.maps.plugin.viewport.data.MultiPuckViewportStateOptions
26+
import com.mapbox.maps.plugin.viewport.state.MultiPuckViewportState
27+
import com.mapbox.maps.plugin.viewport.viewport
2328
import com.mapbox.maps.testapp.R
29+
import com.mapbox.maps.testapp.databinding.ActivityMultiDisplayBinding
30+
import com.mapbox.maps.testapp.databinding.ActivityMultiLocationcomponentBinding
2431
import com.mapbox.maps.testapp.utils.BitmapUtils
25-
import com.mapbox.maps.testapp.utils.LocationComponentUtils
2632
import com.mapbox.maps.testapp.utils.createLocationComponent
2733
import java.util.*
2834

@@ -32,7 +38,7 @@ import java.util.*
3238
@OptIn(MapboxExperimental::class)
3339
class MultipleLocationComponentActivity : AppCompatActivity(), OnMapClickListener {
3440
private lateinit var mapView: MapView
35-
private val journeys = mutableListOf(Journey(speed = 150.0), Journey(speed = 100.0))
41+
private val journeys = mutableListOf(Journey(speed = 150.0, angularSpeed = 150.0), Journey(speed = 100.0, angularSpeed = 150.0))
3642
private val customJourneyLocationProviders = mutableListOf(
3743
CustomJourneyLocationProvider().apply { loadJourney(journeys.first()) },
3844
CustomJourneyLocationProvider().apply { loadJourney(journeys.last()) }
@@ -41,24 +47,26 @@ class MultipleLocationComponentActivity : AppCompatActivity(), OnMapClickListene
4147
mutableListOf(LinkedList<PointAnnotation>(), LinkedList<PointAnnotation>())
4248
private val locationComponents = mutableListOf<LocationComponentPlugin2>()
4349
private lateinit var pointAnnotationManager: PointAnnotationManager
50+
private lateinit var multiPuckViewportState: MultiPuckViewportState
4451

4552
var selectedPuck = 0
4653

4754
@SuppressLint("SetTextI18n")
4855
override fun onCreate(savedInstanceState: Bundle?) {
4956
super.onCreate(savedInstanceState)
50-
mapView = MapView(this)
51-
setContentView(mapView)
52-
mapView.addView(
53-
Button(this).apply {
54-
layoutParams = ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)
57+
val binding = ActivityMultiLocationcomponentBinding.inflate(layoutInflater)
58+
setContentView(binding.root)
59+
mapView = binding.mapView
60+
binding.toggleControlBtn.apply {
61+
text = "Controlling ${if (selectedPuck == 0) "Car" else "Duck"}"
62+
setOnClickListener {
63+
toggleControl()
5564
text = "Controlling ${if (selectedPuck == 0) "Car" else "Duck"}"
56-
setOnClickListener {
57-
toggleControl()
58-
text = "Controlling ${if (selectedPuck == 0) "Car" else "Duck"}"
59-
}
6065
}
61-
)
66+
}
67+
binding.followPucksButton.setOnClickListener {
68+
mapView.viewport.transitionTo(multiPuckViewportState)
69+
}
6270
pointAnnotationManager = mapView.annotations.createPointAnnotationManager()
6371
mapView.getMapboxMap()
6472
.apply {
@@ -97,7 +105,7 @@ class MultipleLocationComponentActivity : AppCompatActivity(), OnMapClickListene
97105
private fun initLocationComponents() {
98106
// Puck with pulsing car
99107
locationComponents.add(
100-
mapView.createLocationComponent(LocationComponentUtils.getNextLocationComponentOptions())
108+
mapView.createLocationComponent(LocationComponentInitOptions.getNextUniqueLocationComponentOptions())
101109
.apply {
102110
setLocationProvider(customJourneyLocationProviders.first())
103111
enabled = true
@@ -110,7 +118,7 @@ class MultipleLocationComponentActivity : AppCompatActivity(), OnMapClickListene
110118
}
111119
)
112120
locationComponents.add(
113-
mapView.createLocationComponent(LocationComponentUtils.getNextLocationComponentOptions())
121+
mapView.createLocationComponent(LocationComponentInitOptions.getNextUniqueLocationComponentOptions())
114122
.apply {
115123
setLocationProvider(customJourneyLocationProviders.first())
116124
enabled = true
@@ -125,7 +133,7 @@ class MultipleLocationComponentActivity : AppCompatActivity(), OnMapClickListene
125133
)
126134
// Puck with pulsing duck
127135
locationComponents.add(
128-
mapView.createLocationComponent(LocationComponentUtils.getNextLocationComponentOptions())
136+
mapView.createLocationComponent(LocationComponentInitOptions.getNextUniqueLocationComponentOptions())
129137
.apply {
130138
setLocationProvider(customJourneyLocationProviders.last())
131139
enabled = true
@@ -138,7 +146,7 @@ class MultipleLocationComponentActivity : AppCompatActivity(), OnMapClickListene
138146
}
139147
)
140148
locationComponents.add(
141-
mapView.createLocationComponent(LocationComponentUtils.getNextLocationComponentOptions())
149+
mapView.createLocationComponent(LocationComponentInitOptions.getNextUniqueLocationComponentOptions())
142150
.apply {
143151
setLocationProvider(customJourneyLocationProviders.last())
144152
enabled = true
@@ -151,6 +159,14 @@ class MultipleLocationComponentActivity : AppCompatActivity(), OnMapClickListene
151159
puckBearingEnabled = true
152160
}
153161
)
162+
multiPuckViewportState = mapView.viewport.makeMultiPuckViewportState(
163+
MultiPuckViewportStateOptions
164+
.Builder()
165+
.bearing(MultiPuckViewportStateBearing.SyncWithLocationPuck(locationComponents.first()))
166+
.padding(EdgeInsets(100.0, 100.0, 100.0, 100.0))
167+
.build(),
168+
locationComponents = locationComponents
169+
)
154170
}
155171

156172
override fun onMapClick(point: Point): Boolean {

app/src/main/java/com/mapbox/maps/testapp/utils/LocationComponentUtils.kt

-14
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,4 @@ public fun MapView.createLocationComponent(locationComponentInitOptions: Locatio
1414
)
1515
this.createPlugin(locationComponentPlugin)
1616
return locationComponent
17-
}
18-
19-
object LocationComponentUtils {
20-
private var customLocationComponentCount = 0
21-
22-
fun getNextLocationComponentOptions() = LocationComponentInitOptions {
23-
puck2DLayerId = "custom_location_component_2d_layer_$customLocationComponentCount"
24-
puck3DLayerId = "custom_location_component_3d_layer_$customLocationComponentCount"
25-
puck3DSourceId = "custom_location_component_3d_source_$customLocationComponentCount"
26-
puck3DSourceId = "custom_location_component_top_icon_image_id_$customLocationComponentCount"
27-
puck3DSourceId = "custom_location_component_shadow_icon_image_id_$customLocationComponentCount"
28-
puck3DSourceId = "custom_location_component_bearing_icon_image_id_$customLocationComponentCount"
29-
customLocationComponentCount++
30-
}
3117
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
tools:context=".examples.MultiDisplayActivity">
7+
8+
<com.mapbox.maps.MapView
9+
android:id="@+id/mapView"
10+
android:layout_width="match_parent"
11+
android:layout_height="match_parent" />
12+
13+
<LinearLayout
14+
android:layout_width="wrap_content"
15+
android:layout_height="wrap_content"
16+
android:layout_centerHorizontal="true"
17+
android:layout_alignParentBottom="true"
18+
android:orientation="vertical"
19+
android:gravity="center"
20+
android:layout_marginBottom="10dp">
21+
22+
<Button
23+
android:id="@+id/toggleControlBtn"
24+
android:layout_width="wrap_content"
25+
android:layout_height="wrap_content"
26+
android:layout_marginBottom="10dp"
27+
android:text="" />
28+
29+
<Button
30+
android:id="@+id/followPucksButton"
31+
android:layout_width="wrap_content"
32+
android:layout_height="wrap_content"
33+
android:text="@string/move_camera" />
34+
35+
</LinearLayout>
36+
</RelativeLayout>

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

+26-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ public class LocationComponentInitOptions private constructor(
4242
topIconImageId, shadowIconImageId, bearingIconImageId
4343
)
4444

45+
public fun toBuilder(): Builder = Builder()
46+
.setPuck2DLayerId(this.puck2DLayerId)
47+
.setPuck3DLayerId(this.puck3DLayerId)
48+
.setPuck3DSourceId(this.puck3DSourceId)
49+
.setTopIconImageId(this.topIconImageId)
50+
.setShadowIconImageId(this.shadowIconImageId)
51+
.setBearingIconImageId(this.bearingIconImageId)
52+
4553
/**
4654
* Composes and builds a [LocationComponentInitOptions] object.
4755
*
@@ -149,12 +157,29 @@ public class LocationComponentInitOptions private constructor(
149157
)
150158
}
151159
}
160+
161+
companion object {
162+
private var customLocationComponentCount = 0
163+
164+
@JvmStatic
165+
fun getNextUniqueLocationComponentOptions() = LocationComponentInitOptions {
166+
puck2DLayerId = "custom_location_component_2d_layer_$customLocationComponentCount"
167+
puck3DLayerId = "custom_location_component_3d_layer_$customLocationComponentCount"
168+
puck3DSourceId = "custom_location_component_3d_source_$customLocationComponentCount"
169+
puck3DSourceId = "custom_location_component_top_icon_image_id_$customLocationComponentCount"
170+
puck3DSourceId =
171+
"custom_location_component_shadow_icon_image_id_$customLocationComponentCount"
172+
puck3DSourceId =
173+
"custom_location_component_bearing_icon_image_id_$customLocationComponentCount"
174+
customLocationComponentCount++
175+
}
176+
}
152177
}
153178

154179
/**
155180
* Creates a [LocationComponentInitOptions] through a DSL-style builder.
156181
*
157-
* @param initializer the intialisation block
182+
* @param initializer the initialisation block
158183
* @return LocationComponentInitOptions
159184
*/
160185
@JvmSynthetic

plugin-viewport/src/main/kotlin/com/mapbox/maps/plugin/viewport/ViewportPluginImpl.kt

+10-8
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,11 @@ import com.mapbox.maps.plugin.animation.Cancelable
1010
import com.mapbox.maps.plugin.animation.MapAnimationOwnerRegistry
1111
import com.mapbox.maps.plugin.animation.camera
1212
import com.mapbox.maps.plugin.delegates.MapDelegateProvider
13-
import com.mapbox.maps.plugin.viewport.data.DefaultViewportTransitionOptions
14-
import com.mapbox.maps.plugin.viewport.data.FollowPuckViewportStateOptions
15-
import com.mapbox.maps.plugin.viewport.data.OverviewViewportStateOptions
16-
import com.mapbox.maps.plugin.viewport.data.ViewportOptions
17-
import com.mapbox.maps.plugin.viewport.data.ViewportStatusChangeReason
18-
import com.mapbox.maps.plugin.viewport.state.FollowPuckViewportState
13+
import com.mapbox.maps.plugin.locationcomponent.LocationComponentPlugin2
14+
import com.mapbox.maps.plugin.viewport.data.*
15+
import com.mapbox.maps.plugin.viewport.state.*
1916
import com.mapbox.maps.plugin.viewport.state.FollowPuckViewportStateImpl
20-
import com.mapbox.maps.plugin.viewport.state.OverviewViewportState
2117
import com.mapbox.maps.plugin.viewport.state.OverviewViewportStateImpl
22-
import com.mapbox.maps.plugin.viewport.state.ViewportState
2318
import com.mapbox.maps.plugin.viewport.transition.DefaultViewportTransition
2419
import com.mapbox.maps.plugin.viewport.transition.DefaultViewportTransitionImpl
2520
import com.mapbox.maps.plugin.viewport.transition.ImmediateViewportTransition
@@ -239,6 +234,13 @@ class ViewportPluginImpl(private val handler: Handler = Handler(Looper.getMainLo
239234
return FollowPuckViewportStateImpl(delegateProvider, options)
240235
}
241236

237+
override fun makeMultiPuckViewportState(
238+
options: MultiPuckViewportStateOptions,
239+
locationComponents: List<LocationComponentPlugin2>
240+
): MultiPuckViewportState {
241+
return MultiPuckViewportStateImpl(delegateProvider, options, locationComponents)
242+
}
243+
242244
/**
243245
* Create an [OverviewViewportState] instance with provided [OverviewViewportStateOptions].
244246
*

0 commit comments

Comments
 (0)