diff --git a/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java b/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java index 8eb9d9036..c7d47e918 100644 --- a/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java +++ b/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java @@ -124,6 +124,7 @@ import com.mapbox.mapboxandroiddemo.examples.styles.MapboxStudioStyleActivity; import com.mapbox.mapboxandroiddemo.examples.styles.ShowHideLayersActivity; import com.mapbox.mapboxandroiddemo.examples.styles.StyleFadeSwitchActivity; +import com.mapbox.mapboxandroiddemo.examples.styles.TextFieldMultipleFormatsActivity; import com.mapbox.mapboxandroiddemo.examples.styles.TransparentBackgroundActivity; import com.mapbox.mapboxandroiddemo.examples.styles.VectorSourceActivity; import com.mapbox.mapboxandroiddemo.examples.styles.ZoomDependentFillColorActivity; @@ -578,6 +579,14 @@ private void initializeModels() { null, R.string.activity_styles_fade_switch_url, false, BuildConfig.MIN_SDK_VERSION)); + exampleItemModels.add(new ExampleItemModel( + R.id.nav_styles, + R.string.activity_styles_text_field_multiple_formats_title, + R.string.activity_styles_text_field_multiple_formats_description, + new Intent(MainActivity.this, TextFieldMultipleFormatsActivity.class), + null, + R.string.activity_styles_text_field_multiple_formats_url, false, BuildConfig.MIN_SDK_VERSION)); + exampleItemModels.add(new ExampleItemModel( R.id.nav_styles, R.string.activity_styles_transparent_background_title, diff --git a/MapboxAndroidDemo/src/main/AndroidManifest.xml b/MapboxAndroidDemo/src/main/AndroidManifest.xml index 8417b75e2..f6a3b27a9 100644 --- a/MapboxAndroidDemo/src/main/AndroidManifest.xml +++ b/MapboxAndroidDemo/src/main/AndroidManifest.xml @@ -396,6 +396,13 @@ android:name="android.support.PARENT_ACTIVITY" android:value="com.mapbox.mapboxandroiddemo.MainActivity" /> + + + diff --git a/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/styles/TextFieldMultipleFormatsActivity.java b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/styles/TextFieldMultipleFormatsActivity.java new file mode 100644 index 000000000..cc9a2c207 --- /dev/null +++ b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/styles/TextFieldMultipleFormatsActivity.java @@ -0,0 +1,126 @@ +package com.mapbox.mapboxandroiddemo.examples.styles; + +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v7.app.AppCompatActivity; + +import com.mapbox.mapboxandroiddemo.R; +import com.mapbox.mapboxsdk.Mapbox; +import com.mapbox.mapboxsdk.maps.MapView; +import com.mapbox.mapboxsdk.maps.MapboxMap; +import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; +import com.mapbox.mapboxsdk.maps.Style; +import com.mapbox.mapboxsdk.style.expressions.Expression; +import com.mapbox.mapboxsdk.style.layers.Layer; + +import static com.mapbox.mapboxsdk.style.expressions.Expression.FormatOption.formatFontScale; +import static com.mapbox.mapboxsdk.style.expressions.Expression.FormatOption.formatTextFont; +import static com.mapbox.mapboxsdk.style.expressions.Expression.format; +import static com.mapbox.mapboxsdk.style.expressions.Expression.formatEntry; +import static com.mapbox.mapboxsdk.style.expressions.Expression.get; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textField; + + +/** + * Use runtime styling to create labels with a SymbolLayer that include multiple + * text fonts and sizes. + */ +public class TextFieldMultipleFormatsActivity extends AppCompatActivity { + + private MapView mapView; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Mapbox access token is configured here. This needs to be called either in your application + // object or in the same activity which contains the mapview. + Mapbox.getInstance(this, getString(R.string.access_token)); + + // This contains the MapView in XML and needs to be called after the access token is configured. + setContentView(R.layout.activity_style_text_field_multiple_formats); + + mapView = findViewById(R.id.mapView); + mapView.onCreate(savedInstanceState); + mapView.getMapAsync(new OnMapReadyCallback() { + @Override + public void onMapReady(@NonNull MapboxMap mapboxMap) { + mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() { + @Override + public void onStyleLoaded(@NonNull final Style style) { + + // Set up label formatting expression + Expression.FormatEntry bigLabel = formatEntry( + get("name_en"), + formatFontScale(1.5), + formatTextFont(new String[] {"Ubuntu Medium", "Arial Unicode MS Regular"}) + ); + + Expression.FormatEntry newLine = formatEntry( + "\n", + formatFontScale(0.5) + ); + + Expression.FormatEntry smallLabel = formatEntry( + get("name"), + formatTextFont(new String[] {"Caveat Regular", "Arial Unicode MS Regular"}) + ); + + Expression format = format(bigLabel, newLine, smallLabel); + + // Retrieve the country label layers from the style and update them with the formatting expression + for (Layer mapLabelLayer : style.getLayers()) { + if (mapLabelLayer.getId().contains("country-label")) { + // Apply formatting expression + mapLabelLayer.setProperties(textField(format)); + } + } + } + }); + } + }); + } + + // Add the mapView lifecycle to the activity's lifecycle methods + @Override + public void onResume() { + super.onResume(); + mapView.onResume(); + } + + @Override + protected void onStart() { + super.onStart(); + mapView.onStart(); + } + + @Override + protected void onStop() { + super.onStop(); + mapView.onStop(); + } + + @Override + public void onPause() { + super.onPause(); + mapView.onPause(); + } + + @Override + public void onLowMemory() { + super.onLowMemory(); + mapView.onLowMemory(); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + mapView.onDestroy(); + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + mapView.onSaveInstanceState(outState); + } +} diff --git a/MapboxAndroidDemo/src/main/res/layout/activity_style_text_field_multiple_formats.xml b/MapboxAndroidDemo/src/main/res/layout/activity_style_text_field_multiple_formats.xml new file mode 100644 index 000000000..6347734a8 --- /dev/null +++ b/MapboxAndroidDemo/src/main/res/layout/activity_style_text_field_multiple_formats.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml b/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml index 8d35bcac4..3ced2960d 100644 --- a/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml @@ -30,6 +30,7 @@ Use an image source to easily display images on the map. Use elevation data to show and customize hills and mountains. Fade map styles in and out based on zoom level. + Use a format expression to style labels in multiple ways. Create a transparent background and fill it with something such as moving water. Select a photo on the device and add it on the map tap location. Use data-driven styling and GeoJSON data to set extrusions\' heights. diff --git a/MapboxAndroidDemo/src/main/res/values/titles_strings.xml b/MapboxAndroidDemo/src/main/res/values/titles_strings.xml index a7d5db057..d8a1f2693 100644 --- a/MapboxAndroidDemo/src/main/res/values/titles_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/titles_strings.xml @@ -27,6 +27,7 @@ Use an image source Hillshading Switch map styles with fade + Multiple text formats Transparent render surface Click to add photo Use GeoJSON data to set extrusion height diff --git a/MapboxAndroidDemo/src/main/res/values/urls_strings.xml b/MapboxAndroidDemo/src/main/res/values/urls_strings.xml index 1a0be800f..f15e32712 100644 --- a/MapboxAndroidDemo/src/main/res/values/urls_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/urls_strings.xml @@ -29,6 +29,7 @@ https://i.imgur.com/I6B6cCE.png https://i.imgur.com/U2OKixV.png https://i.imgur.com/1sPnDx5.png + https://i.imgur.com/OIbBFI5.png https://i.imgur.com/5bYnlp5.png https://i.imgur.com/uPIH5Ck.png http://i.imgur.com/se1z8Wb.png