diff options
author | tobrun <tobrun.van.nuland@gmail.com> | 2018-08-13 11:40:54 +0200 |
---|---|---|
committer | Tobrun <tobrun.van.nuland@gmail.com> | 2018-08-20 19:51:03 +0200 |
commit | b61d9643ee31887a1792684676248c0b3c27da4d (patch) | |
tree | 6f048c49afa3e7d3e6e79d5e260e1d824040f5b1 | |
parent | 7f5d5c85dd4c6c93dfb617a992e15cf6c61bf662 (diff) | |
download | qtlocation-mapboxgl-upstream/tvn-line-gradient-integration.tar.gz |
[android] - add line-gradient example, integrate lineProgress expressionupstream/tvn-line-gradient-integration
8 files changed, 196 insertions, 0 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/expressions/Expression.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/expressions/Expression.java index 1aa0ce9093..67fe9e6e0b 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/expressions/Expression.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/expressions/Expression.java @@ -1144,6 +1144,16 @@ public class Expression { } /** + * Gets the progress along a gradient line. Can only be used in the line-gradient property. + * + * @return expression + * @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-line-progress">Style specification</a> + */ + public static Expression lineProgress() { + return new Expression("line-progress"); + } + + /** * Retrieves an item from an array. * * @param number the index expression diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonOptions.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonOptions.java index 79cde7429c..707c7c43a7 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonOptions.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonOptions.java @@ -89,4 +89,11 @@ public class GeoJsonOptions extends HashMap<String, Object> { return this; } + /** + * + */ + public GeoJsonOptions withLineMetrics(boolean lineMetrics) { + this.put("lineMetrics", lineMetrics); + return this; + } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml index c87e9d09bf..e23b1d64f3 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml @@ -433,6 +433,17 @@ android:value=".activity.FeatureOverviewActivity" /> </activity> <activity + android:name=".activity.style.GradientLineActivity" + android:description="@string/description_gradient_line" + android:label="@string/activity_gradient_line"> + <meta-data + android:name="@string/category" + android:value="@string/category_style" /> + <meta-data + android:name="android.support.PARENT_ACTIVITY" + android:value=".activity.FeatureOverviewActivity" /> + </activity> + <activity android:name=".activity.style.DataDrivenStyleActivity" android:description="@string/description_data_driven_style" android:label="@string/activity_data_driven_style"> diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/GradientLineActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/GradientLineActivity.java new file mode 100644 index 0000000000..45dd29354d --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/GradientLineActivity.java @@ -0,0 +1,117 @@ +package com.mapbox.mapboxsdk.testapp.activity.style; + +import android.graphics.Color; +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import com.mapbox.mapboxsdk.maps.MapView; +import com.mapbox.mapboxsdk.maps.MapboxMap; +import com.mapbox.mapboxsdk.style.expressions.Expression; +import com.mapbox.mapboxsdk.style.layers.LineLayer; +import com.mapbox.mapboxsdk.style.layers.Property; +import com.mapbox.mapboxsdk.style.sources.GeoJsonOptions; +import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; +import com.mapbox.mapboxsdk.testapp.R; +import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils; +import timber.log.Timber; + +import java.io.IOException; + +import static com.mapbox.mapboxsdk.style.expressions.Expression.interpolate; +import static com.mapbox.mapboxsdk.style.expressions.Expression.lineProgress; +import static com.mapbox.mapboxsdk.style.expressions.Expression.linear; +import static com.mapbox.mapboxsdk.style.expressions.Expression.rgb; +import static com.mapbox.mapboxsdk.style.expressions.Expression.stop; +import static com.mapbox.mapboxsdk.style.layers.Property.LINE_CAP_ROUND; +import static com.mapbox.mapboxsdk.style.layers.Property.LINE_JOIN_ROUND; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineCap; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineGradient; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineJoin; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth; + +public class GradientLineActivity extends AppCompatActivity { + + public static final String ID_LINE_SOURCE = "gradient"; + private MapboxMap mapboxMap; + private MapView mapView; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_gradient_line); + + mapView = findViewById(R.id.mapView); + mapView.onCreate(savedInstanceState); + mapView.getMapAsync(map -> { + mapboxMap = map; + + try { + String geoJson = ResourceUtils.readRawResource(GradientLineActivity.this, R.raw.test_line_gradient_feature); + GeoJsonSource source = new GeoJsonSource(ID_LINE_SOURCE, geoJson, new GeoJsonOptions().withLineMetrics(true)); + mapboxMap.addSource(source); + + Expression lineGradientExpression = interpolate( + linear(), lineProgress(), + stop(0f, rgb(0, 0, 255)), + stop(0.5f, rgb(0, 255, 0)), + stop(1f, rgb(255, 0, 0)) + ); + + LineLayer layer = new LineLayer("gradient", ID_LINE_SOURCE) + .withProperties( + lineColor(Color.RED), + lineWidth(10.0f), + lineGradient(lineGradientExpression), + lineCap(LINE_CAP_ROUND), + lineJoin(LINE_JOIN_ROUND) + ); + + mapboxMap.addLayer(layer); + } catch (IOException exception) { + Timber.e(exception); + } + }); + } + + @Override + protected void onStart() { + super.onStart(); + mapView.onStart(); + } + + @Override + protected void onResume() { + super.onResume(); + mapView.onResume(); + } + + @Override + protected void onPause() { + super.onPause(); + mapView.onPause(); + } + + @Override + protected void onStop() { + super.onStop(); + mapView.onStop(); + } + + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + mapView.onSaveInstanceState(outState); + } + + @Override + public void onLowMemory() { + super.onLowMemory(); + mapView.onLowMemory(); + } + + @Override + public void onDestroy() { + super.onDestroy(); + mapView.onDestroy(); + } +} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_gradient_line.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_gradient_line.xml new file mode 100644 index 0000000000..e5db4b63df --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_gradient_line.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="utf-8"?> +<com.mapbox.mapboxsdk.maps.MapView + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + android:id="@id/mapView" + android:layout_width="match_parent" + android:layout_height="match_parent" + app:mapbox_cameraTargetLat="45.38301927899065" + app:mapbox_cameraTargetLng="8.63525390625" + app:mapbox_cameraZoom="7" + app:mapbox_styleUrl="@string/mapbox_style_mapbox_streets"/> diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/raw/test_line_gradient_feature.geojson b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/raw/test_line_gradient_feature.geojson new file mode 100644 index 0000000000..3525259cba --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/raw/test_line_gradient_feature.geojson @@ -0,0 +1,38 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 9.38507080078125, + 46.16936992120204 + ], + [ + 9.07196044921875, + 45.81540082150529 + ], + [ + 9.3878173828125, + 45.85271700071619 + ], + [ + 9.2010498046875, + 45.46783598133375 + ], + [ + 8.876953125, + 44.422011314236634 + ], + [ + 7.635498046875, + 45.07352060670971 + ] + ] + } + } + ] +}
\ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml index bb4e0cfe6d..6c68edc4d2 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml @@ -29,6 +29,7 @@ <string name="description_dynamic_info_window_adapter">Learn how to create a dynamic custom InfoWindow</string> <string name="description_viewpager">Use SupportMapFragments in a ViewPager</string> <string name="description_runtime_style">Adopt the map style on the fly</string> + <string name="description_gradient_line">Show a gradient line layer from a geojson source</string> <string name="description_data_driven_style">Use functions to change the map appearance</string> <string name="description_symbol_layer">Manipulate symbols at runtime</string> <string name="description_custom_sprite">Use a custom sprite in a Symbol Layer</string> diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml index 736c33fe34..ddf3518863 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml @@ -30,6 +30,7 @@ <string name="activity_minmax_zoom">Min/Max Zoom</string> <string name="activity_viewpager">ViewPager</string> <string name="activity_runtime_style">Runtime Style</string> + <string name="activity_gradient_line">Gradient line layer</string> <string name="activity_data_driven_style">Data Driven Style</string> <string name="activity_circle_layer">Circle layer</string> <string name="activity_style_file">Local Style file</string> |