Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 9e47037

Browse files
committed
[android] - add line-gradient example, integrate lineProgress expression
1 parent 7f5d5c8 commit 9e47037

File tree

8 files changed

+216
-1
lines changed

8 files changed

+216
-1
lines changed

platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/expressions/Expression.java

+26
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,32 @@ public static Expression heatmapDensity() {
11431143
return new Expression("heatmap-density");
11441144
}
11451145

1146+
/**
1147+
* Gets the progress along a gradient line. Can only be used in the line-gradient property.
1148+
* <p>
1149+
* Example usage:
1150+
* </p>
1151+
* <pre>
1152+
* {@code
1153+
* LineLayer layer = new LineLayer("layer-id", "source-id");
1154+
* layer.setProperties(
1155+
* lineGradient(interpolate(
1156+
* linear(), lineProgress(),
1157+
* stop(0f, rgb(0, 0, 255)),
1158+
* stop(0.5f, rgb(0, 255, 0)),
1159+
* stop(1f, rgb(255, 0, 0)))
1160+
* )
1161+
* )
1162+
* }
1163+
* </pre>
1164+
*
1165+
* @return expression
1166+
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-line-progress">Style specification</a>
1167+
*/
1168+
public static Expression lineProgress() {
1169+
return new Expression("line-progress");
1170+
}
1171+
11461172
/**
11471173
* Retrieves an item from an array.
11481174
*

platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonOptions.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ public GeoJsonOptions withBuffer(int buffer) {
4444
return this;
4545
}
4646

47+
/**
48+
* Initialises whether to calculate line distance metrics.
49+
*
50+
* @param lineMetrics true to calculate line distance metrics.
51+
* @return the current instance for chaining
52+
*/
53+
public GeoJsonOptions withLineMetrics(boolean lineMetrics) {
54+
this.put("lineMetrics", lineMetrics);
55+
return this;
56+
}
57+
4758
/**
4859
* Douglas-Peucker simplification tolerance (higher means simpler geometries and faster performance).
4960
*
@@ -88,5 +99,4 @@ public GeoJsonOptions withClusterRadius(int clusterRadius) {
8899
this.put("clusterRadius", clusterRadius);
89100
return this;
90101
}
91-
92102
}

platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml

+11
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,17 @@
432432
android:name="android.support.PARENT_ACTIVITY"
433433
android:value=".activity.FeatureOverviewActivity" />
434434
</activity>
435+
<activity
436+
android:name=".activity.style.GradientLineActivity"
437+
android:description="@string/description_gradient_line"
438+
android:label="@string/activity_gradient_line">
439+
<meta-data
440+
android:name="@string/category"
441+
android:value="@string/category_style" />
442+
<meta-data
443+
android:name="android.support.PARENT_ACTIVITY"
444+
android:value=".activity.FeatureOverviewActivity" />
445+
</activity>
435446
<activity
436447
android:name=".activity.style.DataDrivenStyleActivity"
437448
android:description="@string/description_data_driven_style"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.mapbox.mapboxsdk.testapp.activity.style;
2+
3+
import android.graphics.Color;
4+
import android.os.Bundle;
5+
import android.support.v7.app.AppCompatActivity;
6+
import com.mapbox.mapboxsdk.maps.MapView;
7+
import com.mapbox.mapboxsdk.maps.MapboxMap;
8+
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
9+
import com.mapbox.mapboxsdk.style.layers.LineLayer;
10+
import com.mapbox.mapboxsdk.style.sources.GeoJsonOptions;
11+
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
12+
import com.mapbox.mapboxsdk.testapp.R;
13+
import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils;
14+
import timber.log.Timber;
15+
16+
import java.io.IOException;
17+
18+
import static com.mapbox.mapboxsdk.style.expressions.Expression.interpolate;
19+
import static com.mapbox.mapboxsdk.style.expressions.Expression.lineProgress;
20+
import static com.mapbox.mapboxsdk.style.expressions.Expression.linear;
21+
import static com.mapbox.mapboxsdk.style.expressions.Expression.rgb;
22+
import static com.mapbox.mapboxsdk.style.expressions.Expression.stop;
23+
import static com.mapbox.mapboxsdk.style.layers.Property.LINE_CAP_ROUND;
24+
import static com.mapbox.mapboxsdk.style.layers.Property.LINE_JOIN_ROUND;
25+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineCap;
26+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor;
27+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineGradient;
28+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineJoin;
29+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth;
30+
31+
/**
32+
* Activity showcasing applying a gradient coloring to a line layer.
33+
*/
34+
public class GradientLineActivity extends AppCompatActivity implements OnMapReadyCallback {
35+
36+
public static final String LINE_SOURCE = "gradient";
37+
private MapboxMap mapboxMap;
38+
private MapView mapView;
39+
40+
@Override
41+
public void onCreate(Bundle savedInstanceState) {
42+
super.onCreate(savedInstanceState);
43+
setContentView(R.layout.activity_gradient_line);
44+
45+
mapView = findViewById(R.id.mapView);
46+
mapView.onCreate(savedInstanceState);
47+
mapView.getMapAsync(this);
48+
}
49+
50+
@Override
51+
public void onMapReady(MapboxMap map) {
52+
this.mapboxMap = map;
53+
54+
try {
55+
String geoJson = ResourceUtils.readRawResource(GradientLineActivity.this, R.raw.test_line_gradient_feature);
56+
mapboxMap.addSource(new GeoJsonSource(LINE_SOURCE, geoJson, new GeoJsonOptions().withLineMetrics(true)));
57+
mapboxMap.addLayer(new LineLayer("gradient", LINE_SOURCE)
58+
.withProperties(
59+
lineGradient(interpolate(
60+
linear(), lineProgress(),
61+
stop(0f, rgb(0, 0, 255)),
62+
stop(0.5f, rgb(0, 255, 0)),
63+
stop(1f, rgb(255, 0, 0)))
64+
),
65+
lineColor(Color.RED),
66+
lineWidth(10.0f),
67+
lineCap(LINE_CAP_ROUND),
68+
lineJoin(LINE_JOIN_ROUND)
69+
)
70+
);
71+
} catch (IOException exception) {
72+
Timber.e(exception);
73+
}
74+
}
75+
76+
@Override
77+
protected void onStart() {
78+
super.onStart();
79+
mapView.onStart();
80+
}
81+
82+
@Override
83+
protected void onResume() {
84+
super.onResume();
85+
mapView.onResume();
86+
}
87+
88+
@Override
89+
protected void onPause() {
90+
super.onPause();
91+
mapView.onPause();
92+
}
93+
94+
@Override
95+
protected void onStop() {
96+
super.onStop();
97+
mapView.onStop();
98+
}
99+
100+
@Override
101+
public void onSaveInstanceState(Bundle outState) {
102+
super.onSaveInstanceState(outState);
103+
mapView.onSaveInstanceState(outState);
104+
}
105+
106+
@Override
107+
public void onLowMemory() {
108+
super.onLowMemory();
109+
mapView.onLowMemory();
110+
}
111+
112+
@Override
113+
public void onDestroy() {
114+
super.onDestroy();
115+
mapView.onDestroy();
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<com.mapbox.mapboxsdk.maps.MapView
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
android:id="@id/mapView"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
app:mapbox_cameraTargetLat="45.38301927899065"
9+
app:mapbox_cameraTargetLng="8.63525390625"
10+
app:mapbox_cameraZoom="7"
11+
app:mapbox_styleUrl="@string/mapbox_style_mapbox_streets"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [
4+
{
5+
"type": "Feature",
6+
"properties": {},
7+
"geometry": {
8+
"type": "LineString",
9+
"coordinates": [
10+
[
11+
9.38507080078125,
12+
46.16936992120204
13+
],
14+
[
15+
9.07196044921875,
16+
45.81540082150529
17+
],
18+
[
19+
9.3878173828125,
20+
45.85271700071619
21+
],
22+
[
23+
9.2010498046875,
24+
45.46783598133375
25+
],
26+
[
27+
8.876953125,
28+
44.422011314236634
29+
],
30+
[
31+
7.635498046875,
32+
45.07352060670971
33+
]
34+
]
35+
}
36+
}
37+
]
38+
}

platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<string name="description_dynamic_info_window_adapter">Learn how to create a dynamic custom InfoWindow</string>
3030
<string name="description_viewpager">Use SupportMapFragments in a ViewPager</string>
3131
<string name="description_runtime_style">Adopt the map style on the fly</string>
32+
<string name="description_gradient_line">Show a gradient line layer from a geojson source</string>
3233
<string name="description_data_driven_style">Use functions to change the map appearance</string>
3334
<string name="description_symbol_layer">Manipulate symbols at runtime</string>
3435
<string name="description_custom_sprite">Use a custom sprite in a Symbol Layer</string>

platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<string name="activity_minmax_zoom">Min/Max Zoom</string>
3131
<string name="activity_viewpager">ViewPager</string>
3232
<string name="activity_runtime_style">Runtime Style</string>
33+
<string name="activity_gradient_line">Gradient line layer</string>
3334
<string name="activity_data_driven_style">Data Driven Style</string>
3435
<string name="activity_circle_layer">Circle layer</string>
3536
<string name="activity_style_file">Local Style file</string>

0 commit comments

Comments
 (0)