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+ }
0 commit comments