Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
[expressions] add format for text field example
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Erb committed Mar 8, 2019
1 parent 5b66455 commit df07c25
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions MapboxAndroidDemo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,13 @@
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
</activity>
<activity
android:name=".examples.styles.TextFieldMultipleFormatsActivity"
android:label="@string/activity_styles_text_field_multiple_formats_title">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
</activity>
<activity
android:name=".examples.styles.ClickToAddImageActivity"
android:label="@string/activity_styles_click_to_add_image_title">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
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.layers.PropertyFactory.textField;

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 = Expression.formatEntry(
Expression.get("name_en"),
Expression.FormatOption.formatFontScale(1.5),
Expression.FormatOption.formatTextFont(new String[] {"Ubuntu Medium", "Arial Unicode MS Regular"})
);

Expression.FormatEntry newLine = Expression.formatEntry(
"\n",
Expression.FormatOption.formatFontScale(0.5)
);

Expression.FormatEntry smallLabel = Expression.formatEntry(
Expression.get("name"),
Expression.FormatOption.formatTextFont(new String[] {"Caveat Regular", "Arial Unicode MS Regular"})
);

Expression format = Expression.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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".examples.styles.TextFieldMultipleFormatsActivity">

<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="48.4"
mapbox:mapbox_cameraTargetLng="18.49"
mapbox:mapbox_cameraZoom="4.5" />

</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<string name="activity_style_image_source_description">Use an image source to easily display images on the map.</string>
<string name="activity_style_hillshade_description">Use elevation data to show and customize hills and mountains.</string>
<string name="activity_styles_fade_switch_description">Fade map styles in and out based on zoom level.</string>
<string name="activity_styles_text_field_multiple_formats_description">Use a format expression to style labels in multiple ways.</string>
<string name="activity_styles_transparent_background_description">Create a transparent background and fill it with something such as moving water.</string>
<string name="activity_styles_click_to_add_image_description">Select a photo on the device and add it on the map tap location.</string>
<string name="activity_extrusions_catalina_marathon_extrusions_description">Use data-driven styling and GeoJSON data to set extrusions\' heights.</string>
Expand Down
1 change: 1 addition & 0 deletions MapboxAndroidDemo/src/main/res/values/titles_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<string name="activity_style_image_source_title">Use an image source</string>
<string name="activity_styles_hillshade_title">Hillshading</string>
<string name="activity_styles_fade_switch_title">Switch map styles with fade</string>
<string name="activity_styles_text_field_multiple_formats_title">Multiple text formats</string>
<string name="activity_styles_transparent_background_title">Transparent render surface</string>
<string name="activity_styles_click_to_add_image_title">Click to add photo</string>
<string name="activity_extrusions_catalina_marathon_extrusions_title">Use GeoJSON data to set extrusion height</string>
Expand Down
1 change: 1 addition & 0 deletions MapboxAndroidDemo/src/main/res/values/urls_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<string name="activity_style_image_source_url" translatable="false">https://i.imgur.com/I6B6cCE.png</string>
<string name="activity_style_hillshade_url" translatable="false">https://i.imgur.com/U2OKixV.png</string>
<string name="activity_styles_fade_switch_url" translatable="false">https://i.imgur.com/1sPnDx5.png</string>
<string name="activity_styles_text_field_multiple_formats_url" translatable="false">https://i.imgur.com/OIbBFI5.png</string>
<string name="activity_styles_transparent_background_url" translatable="false">https://i.imgur.com/5bYnlp5.png</string>
<string name="activity_styles_click_to_add_image_url" translatable="false">https://i.imgur.com/uPIH5Ck.png</string>
<string name="activity_extrusions_population_density_extrusions_url" translatable="false">http://i.imgur.com/se1z8Wb.png</string>
Expand Down

0 comments on commit df07c25

Please sign in to comment.