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

Commit 4e484b0

Browse files
osanaLangston Smith
authored andcommitted
Variable label placement example (#1066)
1 parent c7cbafd commit 4e484b0

File tree

9 files changed

+246
-5
lines changed

9 files changed

+246
-5
lines changed

MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
import com.mapbox.mapboxandroiddemo.examples.styles.TextFieldFormattingActivity;
139139
import com.mapbox.mapboxandroiddemo.examples.styles.TextFieldMultipleFormatsActivity;
140140
import com.mapbox.mapboxandroiddemo.examples.styles.TransparentBackgroundActivity;
141+
import com.mapbox.mapboxandroiddemo.examples.styles.VariableLabelPlacementActivity;
141142
import com.mapbox.mapboxandroiddemo.examples.styles.VectorSourceActivity;
142143
import com.mapbox.mapboxandroiddemo.examples.styles.ZoomDependentFillColorActivity;
143144
import com.mapbox.mapboxandroiddemo.examples.labs.HomeScreenWidgetActivity;
@@ -650,12 +651,20 @@ private void initializeModels() {
650651
R.string.activity_styles_text_field_formatting_url, true, BuildConfig.MIN_SDK_VERSION));
651652

652653
exampleItemModels.add(new ExampleItemModel(
653-
R.id.nav_styles,
654-
R.string.activity_styles_missing_icon_title,
655-
R.string.activity_styles_missing_icon_description,
656-
new Intent(MainActivity.this, MissingIconActivity.class),
654+
R.id.nav_styles,
655+
R.string.activity_styles_missing_icon_title,
656+
R.string.activity_styles_missing_icon_description,
657+
new Intent(MainActivity.this, MissingIconActivity.class),
658+
null,
659+
R.string.activity_styles_missing_icon_url, true, BuildConfig.MIN_SDK_VERSION));
660+
661+
exampleItemModels.add(new ExampleItemModel(
662+
R.id.nav_styles,
663+
R.string.activity_styles_variable_label_placement_title,
664+
R.string.activity_styles_variable_label_placement_description,
665+
new Intent(MainActivity.this, VariableLabelPlacementActivity.class),
657666
null,
658-
R.string.activity_styles_missing_icon_url, true, BuildConfig.MIN_SDK_VERSION));
667+
R.string.activity_styles_variable_label_placement_url, true, BuildConfig.MIN_SDK_VERSION));
659668

660669
exampleItemModels.add(new ExampleItemModel(
661670
R.id.nav_extrusions,

MapboxAndroidDemo/src/main/AndroidManifest.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,14 @@
525525
android:name="android.support.PARENT_ACTIVITY"
526526
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
527527
</activity>
528+
<activity
529+
android:name=".examples.styles.VariableLabelPlacementActivity"
530+
android:label="@string/activity_styles_variable_label_placement_title"
531+
android:screenOrientation="portrait">]
532+
<meta-data
533+
android:name="android.support.PARENT_ACTIVITY"
534+
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
535+
</activity>
528536
<activity
529537
android:name=".examples.labs.PulsingLayerOpacityColorActivity"
530538
android:label="@string/activity_lab_pulsing_layer_opacity_color_title">
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [{
4+
"type": "Feature",
5+
"properties": {
6+
"description": "Ford's\nTheater",
7+
"icon": "theatre"
8+
},
9+
"geometry": {
10+
"type": "Point",
11+
"coordinates": [-77.038659, 38.931567]
12+
}
13+
}, {
14+
"type": "Feature",
15+
"properties": {
16+
"description": "The\nGaslight",
17+
"icon": "theatre"
18+
},
19+
"geometry": {
20+
"type": "Point",
21+
"coordinates": [-77.003168, 38.894651]
22+
}
23+
}, {
24+
"type": "Feature",
25+
"properties": {
26+
"description": "Horrible\nHarry's",
27+
"icon": "bar"
28+
},
29+
"geometry": {
30+
"type": "Point",
31+
"coordinates": [-77.090372, 38.881189]
32+
}
33+
}, {
34+
"type": "Feature",
35+
"properties": {
36+
"description": "Bike\nParty",
37+
"icon": "bicycle"
38+
},
39+
"geometry": {
40+
"type": "Point",
41+
"coordinates": [-77.052477, 38.943951]
42+
}
43+
}, {
44+
"type": "Feature",
45+
"properties": {
46+
"description": "Rockabilly\nRockstars",
47+
"icon": "music"
48+
},
49+
"geometry": {
50+
"type": "Point",
51+
"coordinates": [-77.031706, 38.914581]
52+
}
53+
}, {
54+
"type": "Feature",
55+
"properties": {
56+
"description": "District\nDrum Tribe",
57+
"icon": "music"
58+
},
59+
"geometry": {
60+
"type": "Point",
61+
"coordinates": [-77.020945, 38.878241]
62+
}
63+
}, {
64+
"type": "Feature",
65+
"properties": {
66+
"description": "Motown\nMemories",
67+
"icon": "music"
68+
},
69+
"geometry": {
70+
"type": "Point",
71+
"coordinates": [-77.007481, 38.876516]
72+
}
73+
}]
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.mapbox.mapboxandroiddemo.examples.styles;
2+
3+
import android.graphics.Color;
4+
import android.os.Bundle;
5+
import android.support.annotation.NonNull;
6+
import android.support.v7.app.AppCompatActivity;
7+
import android.widget.Toast;
8+
9+
import com.mapbox.mapboxandroiddemo.R;
10+
import com.mapbox.mapboxsdk.Mapbox;
11+
import com.mapbox.mapboxsdk.maps.MapView;
12+
import com.mapbox.mapboxsdk.maps.MapboxMap;
13+
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
14+
import com.mapbox.mapboxsdk.maps.Style;
15+
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
16+
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
17+
18+
import static com.mapbox.mapboxsdk.style.expressions.Expression.get;
19+
import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_ANCHOR_BOTTOM;
20+
import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_ANCHOR_LEFT;
21+
import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_ANCHOR_RIGHT;
22+
import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_ANCHOR_TOP;
23+
import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_JUSTIFY_AUTO;
24+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textColor;
25+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textField;
26+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textJustify;
27+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textRadialOffset;
28+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textSize;
29+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textVariableAnchor;
30+
31+
32+
/**
33+
* To increase the chance of high-priority labels staying visible, provide the map
34+
* renderer a list of preferred text anchor positions via
35+
* {@link com.mapbox.mapboxsdk.style.layers.PropertyFactory#textVariableAnchor(String[])}.
36+
*/
37+
public class VariableLabelPlacementActivity extends AppCompatActivity {
38+
39+
private static final String GEOJSON_SRC_ID = "poi_source_id";
40+
private static final String POI_LABELS_LAYER_ID = "poi_labels_layer_id";
41+
private MapView mapView;
42+
43+
@Override
44+
protected void onCreate(Bundle savedInstanceState) {
45+
super.onCreate(savedInstanceState);
46+
47+
// Mapbox access token is configured here. This needs to be called either in your application
48+
// object or in the same activity which contains the mapview.
49+
Mapbox.getInstance(this, getString(R.string.access_token));
50+
51+
// This contains the MapView in XML and needs to be called after the access token is configured.
52+
setContentView(R.layout.activity_style_variable_text_placement);
53+
54+
mapView = findViewById(R.id.mapView);
55+
mapView.onCreate(savedInstanceState);
56+
mapView.getMapAsync(new OnMapReadyCallback() {
57+
@Override
58+
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
59+
60+
GeoJsonSource source = new GeoJsonSource(GEOJSON_SRC_ID);
61+
source.setUrl("asset://poi_places.geojson");
62+
63+
mapboxMap.setStyle(new Style.Builder().fromUrl(Style.LIGHT)
64+
.withSource(source)
65+
// Adds a SymbolLayer to display POI labels
66+
.withLayer(new SymbolLayer(POI_LABELS_LAYER_ID, GEOJSON_SRC_ID)
67+
.withProperties(
68+
textField(get("description")),
69+
textSize(17f),
70+
textColor(Color.RED),
71+
textVariableAnchor(
72+
new String[]{TEXT_ANCHOR_TOP, TEXT_ANCHOR_BOTTOM, TEXT_ANCHOR_LEFT, TEXT_ANCHOR_RIGHT}),
73+
textJustify(TEXT_JUSTIFY_AUTO),
74+
textRadialOffset(0.5f))),
75+
new Style.OnStyleLoaded() {
76+
@Override
77+
public void onStyleLoaded(@NonNull final Style style) {
78+
Toast.makeText(VariableLabelPlacementActivity.this,
79+
getString(R.string.zoom_map_in_and_out_variable_label_instruction),
80+
Toast.LENGTH_SHORT).show();
81+
}
82+
});
83+
}
84+
});
85+
}
86+
87+
// Add the mapView lifecycle to the activity's lifecycle methods
88+
@Override
89+
public void onResume() {
90+
super.onResume();
91+
mapView.onResume();
92+
}
93+
94+
@Override
95+
protected void onStart() {
96+
super.onStart();
97+
mapView.onStart();
98+
}
99+
100+
@Override
101+
protected void onStop() {
102+
super.onStop();
103+
mapView.onStop();
104+
}
105+
106+
@Override
107+
public void onPause() {
108+
super.onPause();
109+
mapView.onPause();
110+
}
111+
112+
@Override
113+
public void onLowMemory() {
114+
super.onLowMemory();
115+
mapView.onLowMemory();
116+
}
117+
118+
@Override
119+
protected void onDestroy() {
120+
super.onDestroy();
121+
mapView.onDestroy();
122+
}
123+
124+
@Override
125+
protected void onSaveInstanceState(Bundle outState) {
126+
super.onSaveInstanceState(outState);
127+
mapView.onSaveInstanceState(outState);
128+
}
129+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent">
6+
7+
<com.mapbox.mapboxsdk.maps.MapView
8+
android:id="@+id/mapView"
9+
android:layout_width="match_parent"
10+
android:layout_height="match_parent"
11+
mapbox:mapbox_cameraTargetLat="38.907"
12+
mapbox:mapbox_cameraTargetLng="-77.04"
13+
mapbox:mapbox_cameraZoom="11.15"/>
14+
15+
</FrameLayout>

MapboxAndroidDemo/src/main/res/values/activity_strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,7 @@
348348
<string name="icon_overlap_explanation">iconAllowOverlap: the icon will be visible even if it collides with other previously drawn symbols</string>
349349
<string name="icon_placement_explanation">iconIgnorePlacement: other symbols can be visible even if \n they collide with the icon</string>
350350

351+
<!-- Variable label placement-->
352+
<string name="zoom_map_in_and_out_variable_label_instruction">Zoom in and out to see red labels try to avoid collisions</string>
353+
351354
</resources>

MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<string name="activity_styles_click_to_add_image_description">Select a photo on the device and add it on the map tap location.</string>
3737
<string name="activity_styles_rotating_anchor_text_description">Adjust the anchor position of SymbolLayer text fields.</string>
3838
<string name="activity_styles_missing_icon_description">Provide an icon when a Style failed to load one.</string>
39+
<string name="activity_styles_variable_label_placement_description">To increase the chance of high-priority labels staying visible, provide the map renderer a list of preferred text anchor positions.</string>
3940
<string name="activity_extrusions_catalina_marathon_extrusions_description">Use data-driven styling and GeoJSON data to set extrusions\' heights.</string>
4041
<string name="activity_extrusions_population_density_extrusions_description">Use extrusions to display 3D building height based on imported vector data.</string>
4142
<string name="activity_extrusions_adjust_extrusions_description">Change the location and color of the light shined on extrusions.</string>

MapboxAndroidDemo/src/main/res/values/titles_strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<string name="activity_styles_click_to_add_image_title">Click to add photo</string>
3434
<string name="activity_styles_rotating_anchor_text_title">Text anchor position</string>
3535
<string name="activity_styles_missing_icon_title">Style with missing icon</string>
36+
<string name="activity_styles_variable_label_placement_title">Variable label placement</string>
3637
<string name="activity_extrusions_catalina_marathon_extrusions_title">Use GeoJSON data to set extrusion height</string>
3738
<string name="activity_extrusions_population_density_extrusions_title">Display 3D building height based on vector data</string>
3839
<string name="activity_extrusions_adjust_extrusions_title">Adjust light location and color</string>

MapboxAndroidDemo/src/main/res/values/urls_strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<string name="activity_styles_click_to_add_image_url" translatable="false">https://i.imgur.com/uPIH5Ck.png</string>
3636
<string name="activity_styles_rotating_anchor_text_url" translatable="false">https://i.imgur.com/w8cP6Wn.png</string>
3737
<string name="activity_styles_missing_icon_url" translatable="false">https://i.imgur.com/lSk2tDB.png</string>
38+
<string name="activity_styles_variable_label_placement_url" translatable="false">https://i.imgur.com/vLgFvlZ.jpg</string>
3839
<string name="activity_extrusions_population_density_extrusions_url" translatable="false">http://i.imgur.com/se1z8Wb.png</string>
3940
<string name="activity_extrusions_catalina_marathon_extrusions_url" translatable="false">http://i.imgur.com/6j848JL.jpg</string>
4041
<string name="activity_extrusions_adjust_extrusions_url" translatable="false">http://i.imgur.com/XNTyIO5.png</string>

0 commit comments

Comments
 (0)