1+ package com.mapbox.maps.testapp.examples
2+
3+ import android.graphics.Color
4+ import android.os.Bundle
5+ import androidx.appcompat.app.AppCompatActivity
6+ import com.google.gson.JsonArray
7+ import com.google.gson.JsonObject
8+ import com.mapbox.geojson.Feature
9+ import com.mapbox.geojson.LineString
10+ import com.mapbox.geojson.Point
11+ import com.mapbox.maps.MapInitOptions
12+ import com.mapbox.maps.MapView
13+ import com.mapbox.maps.MapboxExperimental
14+ import com.mapbox.maps.Style
15+ import com.mapbox.maps.dsl.cameraOptions
16+ import com.mapbox.maps.extension.style.expressions.dsl.generated.atInterpolated
17+ import com.mapbox.maps.extension.style.layers.generated.lineLayer
18+ import com.mapbox.maps.extension.style.layers.properties.generated.LineElevationReference
19+ import com.mapbox.maps.extension.style.layers.properties.generated.LineJoin
20+ import com.mapbox.maps.extension.style.layers.properties.generated.ProjectionName
21+ import com.mapbox.maps.extension.style.projection.generated.projection
22+ import com.mapbox.maps.extension.style.sources.generated.geoJsonSource
23+ import com.mapbox.maps.extension.style.style
24+
25+ /* *
26+ * Example of adding an elevated line to a map.
27+ * This example is based on mapbox-gl-js example: https://docs.mapbox.com/mapbox-gl-js/example/elevated-line/
28+ */
29+ class ElevatedLineActivity : AppCompatActivity () {
30+
31+ @OptIn(MapboxExperimental ::class )
32+ override fun onCreate (savedInstanceState : Bundle ? ) {
33+ super .onCreate(savedInstanceState)
34+ val mapInitOptions = MapInitOptions (
35+ this ,
36+ cameraOptions = CAMERA_OPTIONS ,
37+ )
38+ val mapView = MapView (this , mapInitOptions)
39+ setContentView(mapView)
40+ mapView.mapboxMap.loadStyle(
41+ style(Style .STANDARD ) {
42+ + geoJsonSource(LINE_SOURCE_ID ) {
43+ lineMetrics(true )
44+ feature(createLineStringFeature(COORDINATES , ELEVATIONS ))
45+ }
46+ // add the elevated line layer
47+ + lineLayer(LINE_LAYER_ID , LINE_SOURCE_ID ) {
48+ lineJoin(LineJoin .ROUND )
49+ lineWidth(8.0 )
50+ lineColor(Color .BLUE )
51+ lineEmissiveStrength(1.0 )
52+ lineElevationReference(LineElevationReference .SEA )
53+ lineZOffset(
54+ atInterpolated {
55+ product {
56+ lineProgress()
57+ subtract {
58+ length { get(ELEVATION_PROPERTY_KEY ) }
59+ literal(1 )
60+ }
61+ }
62+ get(ELEVATION_PROPERTY_KEY )
63+ }
64+ )
65+ }
66+ }
67+ )
68+ }
69+
70+ private fun createLineStringFeature (coordinates : List <Point >, elevations : List <Double >): Feature {
71+ val properties = JsonObject ()
72+ // add an array of elevation values.
73+ // the number of values doesn't need to match the number of coordinates.
74+ JsonArray ().apply {
75+ elevations.forEach(::add)
76+ }.let { elevationsArray ->
77+ properties.add(ELEVATION_PROPERTY_KEY , elevationsArray)
78+ }
79+ return Feature .fromGeometry(LineString .fromLngLats(coordinates), properties)
80+ }
81+
82+ companion object {
83+ private const val ELEVATION_PROPERTY_KEY = " elevation"
84+ private const val LINE_SOURCE_ID = " geojson"
85+ private const val LINE_LAYER_ID = " elevated-line"
86+
87+ private val CAMERA_OPTIONS = cameraOptions {
88+ center(Point .fromLngLat(6.7782 , 45.8418 ))
89+ zoom(11.0 )
90+ bearing(- 150.0 )
91+ pitch(62.0 )
92+ projection(ProjectionName .MERCATOR )
93+ }
94+
95+ private val ELEVATIONS = arrayOf(
96+ 4600 , 4600 , 4600 , 4599 , 4598 , 4596 , 4593 , 4590 , 4584 , 4578 , 4569 ,
97+ 4559 , 4547 , 4533 , 4516 , 4497 , 4475 , 4450 , 4422 , 4390 , 4355 , 4316 ,
98+ 4275 , 4227 , 4177 , 4124 , 4068 , 4009 , 3946 , 3880 , 3776 , 3693 , 3599 ,
99+ 3502 , 3398 , 3290 , 3171 , 3052 , 2922 , 2786 , 2642 , 2490 , 2332 , 2170 ,
100+ 1994 , 1810 , 1612 , 1432 , 1216 , 1000
101+ ).map { it.toDouble() }
102+
103+ private val COORDINATES = listOf (
104+ Point .fromLngLat(6.862885 , 45.833563 ),
105+ Point .fromLngLat(6.863605 , 45.846851 ),
106+ Point .fromLngLat(6.859783 , 45.862445 ),
107+ Point .fromLngLat(6.848727 , 45.876361 ),
108+ Point .fromLngLat(6.827155 , 45.892361 ),
109+ Point .fromLngLat(6.802194 , 45.905032 ),
110+ Point .fromLngLat(6.780023 , 45.909602 ),
111+ Point .fromLngLat(6.753605 , 45.906074 ),
112+ Point .fromLngLat(6.728807 , 45.899120 ),
113+ Point .fromLngLat(6.700449 , 45.883872 ),
114+ Point .fromLngLat(6.683772 , 45.863866 ),
115+ Point .fromLngLat(6.684058 , 45.841619 ),
116+ Point .fromLngLat(6.691115 , 45.825417 ),
117+ Point .fromLngLat(6.704446 , 45.813349 ),
118+ Point .fromLngLat(6.720959 , 45.807886 ),
119+ Point .fromLngLat(6.748477 , 45.809517 ),
120+ Point .fromLngLat(6.775554 , 45.817254 ),
121+ Point .fromLngLat(6.791236 , 45.828871 ),
122+ Point .fromLngLat(6.801289 , 45.838797 ),
123+ Point .fromLngLat(6.806307 , 45.849788 ),
124+ Point .fromLngLat(6.803161 , 45.866159 ),
125+ Point .fromLngLat(6.794599 , 45.880461 ),
126+ Point .fromLngLat(6.769846 , 45.890231 ),
127+ Point .fromLngLat(6.744712 , 45.889576 ),
128+ Point .fromLngLat(6.722788 , 45.881677 ),
129+ Point .fromLngLat(6.708097 , 45.868556 ),
130+ Point .fromLngLat(6.699435 , 45.851973 ),
131+ Point .fromLngLat(6.707324 , 45.832980 ),
132+ Point .fromLngLat(6.723743 , 45.822384 ),
133+ Point .fromLngLat(6.739347 , 45.818626 ),
134+ Point .fromLngLat(6.756019 , 45.822069 ),
135+ Point .fromLngLat(6.773963 , 45.832436 ),
136+ Point .fromLngLat(6.785920 , 45.848229 ),
137+ Point .fromLngLat(6.786155 , 45.860521 ),
138+ Point .fromLngLat(6.774430 , 45.870586 ),
139+ Point .fromLngLat(6.749012 , 45.875670 ),
140+ Point .fromLngLat(6.731251 , 45.868501 ),
141+ Point .fromLngLat(6.716033 , 45.853689 ),
142+ Point .fromLngLat(6.714748 , 45.846970 ),
143+ Point .fromLngLat(6.714635 , 45.838934 ),
144+ Point .fromLngLat(6.717850 , 45.832829 ),
145+ Point .fromLngLat(6.724010 , 45.828151 ),
146+ Point .fromLngLat(6.730551 , 45.827333 ),
147+ Point .fromLngLat(6.733951 , 45.829900 ),
148+ Point .fromLngLat(6.735957 , 45.834154 ),
149+ Point .fromLngLat(6.735286 , 45.839871 ),
150+ Point .fromLngLat(6.734471 , 45.843933 ),
151+ Point .fromLngLat(6.730893 , 45.847233 ),
152+ Point .fromLngLat(6.728550 , 45.847899 ),
153+ Point .fromLngLat(6.726590 , 45.847822 ),
154+ Point .fromLngLat(6.724876 , 45.846455 ),
155+ Point .fromLngLat(6.725096 , 45.843900 ),
156+ Point .fromLngLat(6.726635 , 45.841201 ),
157+ Point .fromLngLat(6.728074 , 45.837041 ),
158+ Point .fromLngLat(6.727822 , 45.834292 ),
159+ )
160+ }
161+ }
0 commit comments