Skip to content

Commit dd31f57

Browse files
natiginfogithub-actions[bot]
authored andcommitted
[maps-android] elevated line example (#4962)
GitOrigin-RevId: a7e481836d9b32c8c2248535c5e7ddebf5a6d3b7
1 parent 0322564 commit dd31f57

File tree

8 files changed

+365
-14
lines changed

8 files changed

+365
-14
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
android:theme="@style/AppTheme"
1818
android:usesCleartextTraffic="true"
1919
tools:ignore="GoogleAppIndexingWarning,UnusedAttribute">
20-
21-
<profileable android:shell="true"/>
20+
<profileable android:shell="true" />
2221

2322
<activity
2423
android:name=".ExampleOverviewActivity"
@@ -144,7 +143,7 @@
144143
android:name=".examples.java.RuntimeStylingJavaActivity"
145144
android:description="@string/description_dsl_styling"
146145
android:exported="true"
147-
android:label="@string/activity_java_runtime_dsl_styling" >
146+
android:label="@string/activity_java_runtime_dsl_styling">
148147
<meta-data
149148
android:name="android.support.PARENT_ACTIVITY"
150149
android:value=".ExampleOverviewActivity" />
@@ -341,7 +340,6 @@
341340
android:name="android.support.PARENT_ACTIVITY"
342341
android:value=".ExampleOverviewActivity" />
343342
</activity>
344-
345343
<activity
346344
android:name=".examples.terrain3D.Lights3DActivity"
347345
android:description="@string/description_lights3d"
@@ -608,7 +606,6 @@
608606
android:name="android.support.PARENT_ACTIVITY"
609607
android:value=".ExampleOverviewActivity" />
610608
</activity>
611-
612609
<activity
613610
android:name=".examples.OngoingAnimationActivity"
614611
android:description="@string/description_animate_camera_during_gesture"
@@ -849,7 +846,6 @@
849846
android:name="android.support.PARENT_ACTIVITY"
850847
android:value=".ExampleOverviewActivity" />
851848
</activity>
852-
853849
<activity
854850
android:name=".examples.snapshotter.MapViewSnapshotActivity"
855851
android:description="@string/description_view_snapshot"
@@ -862,7 +858,6 @@
862858
android:name="android.support.PARENT_ACTIVITY"
863859
android:value=".ExampleOverviewActivity" />
864860
</activity>
865-
866861
<activity
867862
android:name=".examples.customlayer.TriangleCustomLayerActivity"
868863
android:description="@string/description_triangle_custom_layer"
@@ -920,6 +915,7 @@
920915
<intent-filter>
921916
<action android:name="android.service.wallpaper.WallpaperService" />
922917
</intent-filter>
918+
923919
<meta-data
924920
android:name="android.service.wallpaper"
925921
android:resource="@xml/map_wallpaper" />
@@ -1197,8 +1193,7 @@
11971193
<meta-data
11981194
android:name="android.support.PARENT_ACTIVITY"
11991195
android:value=".ExampleOverviewActivity" />
1200-
</activity>
1201-
<!-- View annotations -->
1196+
</activity> <!-- View annotations -->
12021197
<activity
12031198
android:name=".examples.markersandcallouts.viewannotation.ViewAnnotationShowcaseActivity"
12041199
android:description="@string/description_view_annotation"
@@ -1258,8 +1253,7 @@
12581253
<meta-data
12591254
android:name="android.support.PARENT_ACTIVITY"
12601255
android:value=".ExampleOverviewActivity" />
1261-
</activity>
1262-
<!-- -->
1256+
</activity> <!-- -->
12631257
<activity
12641258
android:name=".examples.viewport.ViewportShowcaseActivity"
12651259
android:description="@string/description_viewport"
@@ -1313,8 +1307,8 @@
13131307
android:exported="true" />
13141308
<activity
13151309
android:name=".EmptyFragmentActivity"
1316-
android:theme="@android:style/Theme.Material.Light"
1317-
android:exported="true" />
1310+
android:exported="true"
1311+
android:theme="@android:style/Theme.Material.Light" />
13181312
<activity
13191313
android:name=".examples.SecondaryDisplayPresentationActivity"
13201314
android:description="@string/description_secondary_display_presentation"
@@ -1339,9 +1333,11 @@
13391333
android:name="android.support.PARENT_ACTIVITY"
13401334
android:value=".ExampleOverviewActivity" />
13411335
</activity>
1336+
13421337
<meta-data
13431338
android:name="com.mapbox.maps.ThreadChecker"
13441339
android:value="true" />
1340+
13451341
<activity
13461342
android:name=".examples.terrain3D.ModelLayerActivity"
13471343
android:description="@string/description_model_layer"
@@ -1450,5 +1446,17 @@
14501446
android:name="android.support.PARENT_ACTIVITY"
14511447
android:value=".ExampleOverviewActivity" />
14521448
</activity>
1449+
<activity
1450+
android:name=".examples.ElevatedLineActivity"
1451+
android:description="@string/description_elevated_line"
1452+
android:label="@string/activity_elevated_line"
1453+
android:exported="true">
1454+
<meta-data
1455+
android:name="@string/category"
1456+
android:value="@string/category_3D" />
1457+
<meta-data
1458+
android:name="android.support.PARENT_ACTIVITY"
1459+
android:value=".ExampleOverviewActivity" />
1460+
</activity>
14531461
</application>
1454-
</manifest>
1462+
</manifest>
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,5 @@
113113
<string name="description_location_component_model_animation">Animate 3D location puck on the map</string>
114114
<string name="description_precipitation">Showcase rain and snow effects.</string>
115115
<string name="description_color_theme">Showcase color theme.</string>
116+
<string name="description_elevated_line">Showcase line elevation</string>
116117
</resources>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,5 @@
115115
<string name="activity_location_component_model_animation">Location component model animation</string>
116116
<string name="activity_precipitation">Precipitations example</string>
117117
<string name="activity_color_theme">Color theme example</string>
118+
<string name="activity_elevated_line">Elevated line example</string>
118119
</resources>

compose-app/src/main/AndroidManifest.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,16 @@
326326
android:name="@string/category"
327327
android:value="@string/category_styles" />
328328
</activity>
329+
<activity
330+
android:name=".examples.style.ElevatedLineActivity"
331+
android:description="@string/description_elevated_line"
332+
android:exported="true"
333+
android:label="@string/activity_elevated_line"
334+
android:parentActivityName=".examples.style.ExampleOverviewActivity">
335+
<meta-data
336+
android:name="@string/category"
337+
android:value="@string/category_styles" />
338+
</activity>
329339
</application>
330340

331341
</manifest>

0 commit comments

Comments
 (0)