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

Commit e46eeeb

Browse files
committed
[android] - add line-gradient example, integrate lineProgress expression
1 parent 19e5c4d commit e46eeeb

File tree

8 files changed

+196
-0
lines changed

8 files changed

+196
-0
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,16 @@ 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+
*
1149+
* @return expression
1150+
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-line-progress">Style specification</a>
1151+
*/
1152+
public static Expression lineProgress() {
1153+
return new Expression("line-progress");
1154+
}
1155+
11461156
/**
11471157
* Retrieves an item from an array.
11481158
*

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

+7
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,11 @@ public GeoJsonOptions withClusterRadius(int clusterRadius) {
8989
return this;
9090
}
9191

92+
/**
93+
*
94+
*/
95+
public GeoJsonOptions withLineMetrics(boolean lineMetrics) {
96+
this.put("lineMetrics", lineMetrics);
97+
return this;
98+
}
9299
}

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.style.expressions.Expression;
9+
import com.mapbox.mapboxsdk.style.layers.LineLayer;
10+
import com.mapbox.mapboxsdk.style.layers.Property;
11+
import com.mapbox.mapboxsdk.style.sources.GeoJsonOptions;
12+
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
13+
import com.mapbox.mapboxsdk.testapp.R;
14+
import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils;
15+
import timber.log.Timber;
16+
17+
import java.io.IOException;
18+
19+
import static com.mapbox.mapboxsdk.style.expressions.Expression.interpolate;
20+
import static com.mapbox.mapboxsdk.style.expressions.Expression.lineProgress;
21+
import static com.mapbox.mapboxsdk.style.expressions.Expression.linear;
22+
import static com.mapbox.mapboxsdk.style.expressions.Expression.rgb;
23+
import static com.mapbox.mapboxsdk.style.expressions.Expression.stop;
24+
import static com.mapbox.mapboxsdk.style.layers.Property.LINE_CAP_ROUND;
25+
import static com.mapbox.mapboxsdk.style.layers.Property.LINE_JOIN_ROUND;
26+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineCap;
27+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor;
28+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineGradient;
29+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineJoin;
30+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth;
31+
32+
public class GradientLineActivity extends AppCompatActivity {
33+
34+
public static final String ID_LINE_SOURCE = "gradient";
35+
private MapboxMap mapboxMap;
36+
private MapView mapView;
37+
38+
@Override
39+
public void onCreate(Bundle savedInstanceState) {
40+
super.onCreate(savedInstanceState);
41+
setContentView(R.layout.activity_gradient_line);
42+
43+
mapView = findViewById(R.id.mapView);
44+
mapView.onCreate(savedInstanceState);
45+
mapView.getMapAsync(map -> {
46+
mapboxMap = map;
47+
48+
try {
49+
String geoJson = ResourceUtils.readRawResource(GradientLineActivity.this, R.raw.test_line_gradient_feature);
50+
GeoJsonSource source = new GeoJsonSource(ID_LINE_SOURCE, geoJson, new GeoJsonOptions().withLineMetrics(true));
51+
mapboxMap.addSource(source);
52+
53+
Expression lineGradientExpression = interpolate(
54+
linear(), lineProgress(),
55+
stop(0f, rgb(0, 0, 255)),
56+
stop(0.5f, rgb(0, 255, 0)),
57+
stop(1f, rgb(255, 0, 0))
58+
);
59+
60+
LineLayer layer = new LineLayer("gradient", ID_LINE_SOURCE)
61+
.withProperties(
62+
lineColor(Color.RED),
63+
lineWidth(10.0f),
64+
lineGradient(lineGradientExpression),
65+
lineCap(LINE_CAP_ROUND),
66+
lineJoin(LINE_JOIN_ROUND)
67+
);
68+
69+
mapboxMap.addLayer(layer);
70+
} catch (IOException exception) {
71+
Timber.e(exception);
72+
}
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)