Examples
Getting started (3)Create a simple map viewDynamically build a map viewSupport map fragmentDynamic styling (30)HillshadingAdd a vector tile sourceAdd a new layer below labelsAnimate marker positionAdjust a layer's opacityShow time lapseAnimated image source (GIF)Icon update based on API responseChange a layer's colorChange a map's languageColor dependent on zoom levelClick to add photoIndoor MapMarker following routeStyle with missing iconSymbol layer iconsPicture in picturePulsing layer opacityAdd a WMS sourceText anchor positionButton interaction stylingOpacity fadeShow and hide layersSymbolLayer iconsAdjust text labelsMultiple text formatsTransparent render surfaceUse an image sourceAnimated icon movementVariable label placementExtrusions (5)Adjust light location and colorDisplay 3D building height based on vector dataExtrude polygons for 3D indoor mappingRotate and tilt with 3D buildingsUse GeoJSON data to set extrusion heightCamera (5)Animate the map cameraFit camera in bounding boxRestrict map panningRotating cameraZoom to show a cluster's leavesData visualization (33)Baseball spray chartDisplay water depthBiometric fingerprintCalendar integrationCircle icon toggleCircleLayer clustersCircle radiusCircle to icon transitionCreate a line layerCreate hotspots from pointsData time lapseShow heatmap dataDraw a GeoJSON lineDraw a polygon with holesDraw a polygonDraw multiple geometriesJoin local JSON data with vector tile geometriesLine gradientLine behind moving iconMultiple expressionsIcon setting based on Feature propertyOutlined polygon holeSatellite land selectShared preferencesSpinning iconStyle circles categoricallyStyle lines using an identity property functionToggle collision detectionSymbolLayer clusteringSymbol layer info windowZoom-based icon switchUpdate a choropleth layer by zoom levelStyling heatmapsOffline (3)Cache managementA simple offline mapOffline managerQuerying the map (3)Building outlineQuery a map featureSelect a buildingMap style (4)Style attributionLocal style or custom raster styleDefault stylesMapbox Studio styleUser interaction (12)Click on single layerFeature countDrawing search areaHighlighted lineSymbol layer icon size changeInset mapLocation pickerBackground fogRecyclerView DirectionsSearch again in an areaSnaking directionsRecyclerView interactionImage generation (2)Snapshot NotificationShare snapshot imageDevice location (7)Track device locationBasic pulsing locationLocation camera optionsCustom pulsing locationCustomized location iconShow a user's location on a map fragmentShow a user's location

Text anchor position

activity_dds_style_rotating_text_anchor
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="32.562100"
mapbox:mapbox_cameraTargetLng="-101.264445"
mapbox:mapbox_cameraZoom="3.4"
mapbox:mapbox_cameraZoomMax="5.38"
mapbox:mapbox_cameraZoomMin="3.4" />
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:layout_marginTop="24dp">
<TextView
android:id="@+id/anchor_position_textview"
android:layout_width="wrap_content"
android:textColor="@color/black_semi_transparent"
android:layout_height="wrap_content"
android:padding="8dp" />
</androidx.cardview.widget.CardView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/switch_anchor_position_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/ic_cycle_through"
mapbox:fabSize="normal" />
</FrameLayout>
RotatingTextAnchorPositionActivity.java
package com.mapbox.mapboxandroiddemo.examples.styles;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
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.layers.Layer;
import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_ANCHOR_BOTTOM;
import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_ANCHOR_BOTTOM_LEFT;
import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_ANCHOR_BOTTOM_RIGHT;
import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_ANCHOR_CENTER;
import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_ANCHOR_LEFT;
import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_ANCHOR_RIGHT;
import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_ANCHOR_TOP;
import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_ANCHOR_TOP_LEFT;
import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_ANCHOR_TOP_RIGHT;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textAllowOverlap;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textAnchor;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textIgnorePlacement;
/**
* Use runtime-styling to adjust the state label SymbolLayer's textField anchor position.
*/
public class RotatingTextAnchorPositionActivity extends AppCompatActivity {
private MapView mapView;
private Layer stateLabelSymbolLayer;
private int index = 0;
private TextView anchorPositionTextView;
private static final String[] anchorOptions = new String[] {
TEXT_ANCHOR_CENTER, // The center of the text is placed closest to the anchor.
TEXT_ANCHOR_LEFT, // The left side of the text is placed closest to the anchor.
TEXT_ANCHOR_RIGHT, // The right side of the text is placed closest to the anchor.
TEXT_ANCHOR_TOP, // The top of the text is placed closest to the anchor.
TEXT_ANCHOR_BOTTOM, // The bottom of the text is placed closest to the anchor.
TEXT_ANCHOR_TOP_LEFT, // The top left corner of the text is placed closest to the anchor.
TEXT_ANCHOR_TOP_RIGHT, // The top right corner of the text is placed closest to the anchor.
TEXT_ANCHOR_BOTTOM_LEFT, // The bottom left corner of the text is placed closest to the anchor.
TEXT_ANCHOR_BOTTOM_RIGHT, // The bottom right corner of the text is placed closest to the anchor.
};
private static final String[] anchorDescriptions = new String[] {
"The center of the text is placed closest to the anchor.",
"The left side of the text is placed closest to the anchor.",
"The right side of the text is placed closest to the anchor.",
"The top of the text is placed closest to the anchor.",
"The bottom of the text is placed closest to the anchor.",
"The top left corner of the text is placed closest to the anchor.",
"The top right corner of the text is placed closest to the anchor.",
"The bottom left corner of the text is placed closest to the anchor.",
"The bottom right corner of the text is placed closest to the anchor."
};
@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_dds_style_rotating_text_anchor);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
stateLabelSymbolLayer = style.getLayer("state-label");
if (stateLabelSymbolLayer != null) {
// Use runtime-styling to ignore collision and allow overlap, so that adjusted text in this example,
// is more easily visible.
stateLabelSymbolLayer.setProperties(
textIgnorePlacement(true),
textAllowOverlap(true)
);
anchorPositionTextView = findViewById(R.id.anchor_position_textview);
setTextView(anchorOptions[index], anchorDescriptions[index]);
findViewById(R.id.switch_anchor_position_fab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
index++;
if (index == anchorOptions.length - 1) {
index = 0;
}
// Use runtime styling to adjust the SymbolLayer's textAnchor position
stateLabelSymbolLayer.setProperties(textAnchor(anchorOptions[index]));
setTextView(anchorOptions[index], anchorDescriptions[index]);
}
});
}
}
});
}
});
}
private void setTextView(String anchorOption, String anchorDescription) {
anchorPositionTextView.setText(String.format(getString(R.string.position_textview),
anchorOption, anchorDescription));
}
// 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);
}
}
Was this page helpful?