Skip to content

Commit 76a9843

Browse files
MBSDK-2282: zoom to bounds of a set of locations
1 parent d518451 commit 76a9843

File tree

5 files changed

+199
-1
lines changed

5 files changed

+199
-1
lines changed

android/MapsSDKExamplesKotlin/app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
<activity
7070
android:name=".SampleRestrictUserInteractionActivity"
7171
android:exported="false" />
72+
<activity
73+
android:name=".SampleFramingActivity"
74+
android:exported="false" />
7275
<activity
7376
android:name=".MainActivity"
7477
android:exported="false"

android/MapsSDKExamplesKotlin/app/src/main/java/com/trimblemaps/mapssdkexampleskotlin/MainActivity.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ class MainActivity : Activity() {
3737
Example(Constants.REGIONAL_MAP, R.drawable.van_nav_splash, SampleRegionalMapActivity::class.java),
3838
Example(Constants.HIGHLIGHT_FEATURES, R.drawable.general_splash,
3939
SampleHighlightFeaturesAfterPanningActivity::class.java),
40-
Example(Constants.RESTRICT_INTERACTION, R.drawable.van_nav_splash, SampleRestrictUserInteractionActivity::class.java)
40+
Example(Constants.RESTRICT_INTERACTION, R.drawable.van_nav_splash, SampleRestrictUserInteractionActivity::class
41+
.java),
42+
Example(Constants.FRAME_LOCATIONS, R.drawable.general_splash, SampleFramingActivity::class.java)
4143
)
4244

4345
private lateinit var binding: ActivityMainBinding
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package com.trimblemaps.mapssdkexampleskotlin
2+
3+
import android.app.Activity
4+
import android.graphics.Color
5+
import android.os.Bundle
6+
import android.view.View
7+
import android.widget.Button
8+
import com.trimblemaps.geojson.FeatureCollection
9+
import com.trimblemaps.geojson.LineString
10+
import com.trimblemaps.geojson.Point
11+
import com.trimblemaps.mapsdk.TrimbleMaps
12+
import com.trimblemaps.mapsdk.camera.CameraPosition
13+
import com.trimblemaps.mapsdk.camera.CameraUpdate
14+
import com.trimblemaps.mapsdk.geometry.LatLng
15+
import com.trimblemaps.mapsdk.maps.MapView
16+
import com.trimblemaps.mapsdk.maps.Style
17+
import com.trimblemaps.mapsdk.maps.TrimbleMapsMap
18+
import com.trimblemaps.mapsdk.style.layers.LineLayer
19+
import com.trimblemaps.mapsdk.style.layers.PropertyFactory
20+
import com.trimblemaps.mapsdk.style.sources.GeoJsonSource
21+
import com.trimblemaps.mapsdk.plugins.places.common.utils.CameraUtils
22+
import java.io.InputStreamReader
23+
24+
class SampleFramingActivity : Activity(), Style.OnStyleLoaded {
25+
private var mapView: MapView? = null
26+
private var map: TrimbleMapsMap? = null
27+
private var currentIndex = 0
28+
private val SOURCE_ID = "linesource"
29+
private val LAYER_ID = "linelayer"
30+
private lateinit var allLines: List<LineString>
31+
32+
override fun onCreate(savedInstanceState: Bundle?) {
33+
super.onCreate(savedInstanceState)
34+
35+
// Get an instance of the map, done before the layout is set.
36+
TrimbleMaps.getInstance(this)
37+
setContentView(R.layout.activity_sample_framing)
38+
39+
// Set up the MapView from the layout
40+
mapView = findViewById<View>(R.id.mapView) as MapView
41+
42+
// the onMapReadyCallback is fired when the map is ready to be worked with
43+
mapView?.getMapAsync { trimbleMapsMap ->
44+
map = trimbleMapsMap
45+
val position = CameraPosition.Builder()
46+
.target(LatLng(40.60902838712187, -97.73800045737227))
47+
.zoom(2.5)
48+
.build()
49+
map?.cameraPosition = position
50+
map?.setStyle(Style.MOBILE_DAY, this@SampleFramingActivity)
51+
}
52+
53+
// When the button is clicked, cycle through the lines using trimbleMapsMap.moveCamera
54+
findViewById<Button>(R.id.btn_cycleLocations).setOnClickListener {
55+
map?.let { trimbleMapsMap ->
56+
if (allLines.isNotEmpty()) {
57+
val selectedLine = allLines[currentIndex]
58+
val pointList = selectedLine.coordinates().map { point ->
59+
Point.fromLngLat(point.longitude(), point.latitude())
60+
}
61+
val update: CameraUpdate? = CameraUtils.frameLocationOrPolygon(
62+
null, // No specific location to frame
63+
pointList
64+
)
65+
if (update != null) {
66+
trimbleMapsMap.moveCamera(update)
67+
}
68+
currentIndex = (currentIndex + 1) % allLines.size
69+
}
70+
}
71+
}
72+
73+
// When the button is clicked, cycle through the lines using trimbleMapsMap.easeCamera
74+
findViewById<Button>(R.id.btn_easeCamera).setOnClickListener {
75+
map?.let { trimbleMapsMap ->
76+
if (allLines.isNotEmpty()) {
77+
val selectedLine = allLines[currentIndex]
78+
val pointList = selectedLine.coordinates().map { point ->
79+
Point.fromLngLat(point.longitude(), point.latitude())
80+
}
81+
val update: CameraUpdate? = CameraUtils.frameLocationOrPolygon(
82+
null, // No specific location to frame
83+
pointList
84+
)
85+
if (update != null) {
86+
trimbleMapsMap.easeCamera(update)
87+
}
88+
currentIndex = (currentIndex + 1) % allLines.size
89+
}
90+
}
91+
}
92+
}
93+
94+
override fun onStyleLoaded(style: Style) {
95+
// Load lines from the GeoJson file
96+
try {
97+
val inputStream = assets.open("lines.json")
98+
val geoJson = InputStreamReader(inputStream).readText()
99+
val featureCollection = FeatureCollection.fromJson(geoJson)
100+
allLines = featureCollection.features()?.mapNotNull { feature ->
101+
feature.geometry() as? LineString
102+
} ?: emptyList()
103+
104+
// Create a GeoJsonSource with the loaded data
105+
val source = GeoJsonSource(SOURCE_ID, featureCollection)
106+
style.addSource(source)
107+
108+
// Create a LineLayer to display our source information.
109+
val lineLayer = LineLayer(LAYER_ID, SOURCE_ID)
110+
lineLayer.setProperties(
111+
PropertyFactory.lineWidth(6f),
112+
PropertyFactory.lineColor(Color.BLUE),
113+
PropertyFactory.lineOpacity(.8f)
114+
)
115+
116+
// Add the layer
117+
style.addLayer(lineLayer)
118+
} catch (e: Exception) {
119+
e.printStackTrace()
120+
}
121+
}
122+
123+
override fun onStart() {
124+
super.onStart()
125+
mapView?.onStart()
126+
}
127+
128+
override fun onResume() {
129+
super.onResume()
130+
mapView?.onResume()
131+
}
132+
133+
override fun onPause() {
134+
super.onPause()
135+
mapView?.onPause()
136+
}
137+
138+
override fun onStop() {
139+
super.onStop()
140+
mapView?.onStop()
141+
}
142+
143+
override fun onLowMemory() {
144+
super.onLowMemory()
145+
mapView?.onLowMemory()
146+
}
147+
148+
override fun onDestroy() {
149+
super.onDestroy()
150+
mapView?.onDestroy()
151+
}
152+
153+
override fun onSaveInstanceState(outState: Bundle) {
154+
if (outState != null) {
155+
super.onSaveInstanceState(outState)
156+
mapView?.onSaveInstanceState(outState)
157+
}
158+
}
159+
}

android/MapsSDKExamplesKotlin/app/src/main/java/com/trimblemaps/mapssdkexampleskotlin/shared/Constants.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ object Constants {
2121
const val REGIONAL_MAP = "Regional Map"
2222
const val HIGHLIGHT_FEATURES = "Highlight Features"
2323
const val RESTRICT_INTERACTION = "Restrict Interaction"
24+
const val FRAME_LOCATIONS = "Frame Locations"
2425
}
2526

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:layout_width="match_parent"
3+
android:layout_height="match_parent"
4+
android:orientation="vertical">
5+
6+
<com.trimblemaps.mapsdk.maps.MapView
7+
android:id="@+id/mapView"
8+
android:layout_width="match_parent"
9+
android:layout_height="0dp"
10+
android:layout_weight="1" />
11+
12+
<LinearLayout
13+
android:layout_width="match_parent"
14+
android:layout_height="wrap_content"
15+
android:orientation="horizontal">
16+
17+
<Button
18+
android:id="@+id/btn_cycleLocations"
19+
android:layout_width="0dp"
20+
android:layout_height="wrap_content"
21+
android:layout_weight="1"
22+
android:text="moveCamera()"
23+
android:textAllCaps="false" />
24+
25+
<Button
26+
android:id="@+id/btn_easeCamera"
27+
android:layout_width="0dp"
28+
android:layout_height="wrap_content"
29+
android:layout_weight="1"
30+
android:text="easeCamera()"
31+
android:textAllCaps="false" />
32+
</LinearLayout>
33+
</LinearLayout>

0 commit comments

Comments
 (0)