Examples
Getting started (3)Create a simple map viewDynamically build a map viewSupport map fragmentDynamic styling (30)Add a new layer below labelsHillshadingAdd a vector tile sourceAdd a WMS sourceShow time lapseAnimate marker positionAnimated 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 routeSymbol layer iconsStyle with missing iconPicture in picturePulsing layer opacityButton interaction stylingOpacity fadeShow and hide layersSymbolLayer iconsAdjust text labelsMultiple text formatsUse an image sourceTransparent render surfaceAnimated icon movementVariable label placementAdjust a layer's opacityText anchor positionExtrusions (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)Display water depthBiometric fingerprintBaseball spray chartCalendar 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 propertyStyling heatmapsOutlined polygon holeSatellite land selectShared preferencesSpinning iconStyle circles categoricallyStyle lines using an identity property functionToggle collision detectionSymbol layer info windowSymbolLayer clusteringZoom-based icon switchUpdate a choropleth layer by zoom levelQuerying the map (3)Building outlineQuery a map featureSelect a buildingOffline (3)Cache managementA simple offline mapOffline managerMap style (4)Style attributionMapbox Studio styleDefault stylesLocal style or custom raster styleUser interaction (12)Click on single layerFeature countDrawing search areaSymbol layer icon size changeHighlighted lineInset mapBackground fogLocation pickerRecyclerView DirectionsRecyclerView interactionSearch again in an areaSnaking directionsImage generation (2)Snapshot NotificationShare snapshot imageDevice location (5)Track device locationLocation camera optionsCustomized location iconShow a user's location on a map fragmentShow a user's location

Button interaction styling

activity_styles_runtime_styling
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mapbox.mapboxandroiddemo.examples.styles.RuntimeStylingActivity">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="19.948045"
mapbox:mapbox_cameraTargetLng="-84.654463"
mapbox:mapbox_cameraZoom="3.371717" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_swap_horiz_white_24dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
mapbox:layout_constraintBottom_toBottomOf="parent"
mapbox:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
RuntimeStylingActivity.java
package com.mapbox.mapboxandroiddemo.examples.styles;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
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 com.mapbox.mapboxsdk.style.layers.PropertyFactory;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
/**
* Change various properties of a map based on user interaction and other runtime situations.
*/
public class RuntimeStylingActivity extends AppCompatActivity {
private MapView mapView;
private Layer waterLayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, getString(R.string.access_token));
setContentView(R.layout.activity_styles_runtime_styling);
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 final Style style) {
waterLayer = style.getLayer("water");
findViewById(R.id.floatingActionButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (waterLayer != null) {
waterLayer.setProperties(PropertyFactory.fillColor(Color.parseColor("#023689"))
);
}
for (Layer singleMapLayer : style.getLayers()) {
if (singleMapLayer.getId().contains("water-") && !singleMapLayer.getId().equals("water-shadow")) {
singleMapLayer.setProperties(
PropertyFactory.textHaloBlur(10f),
PropertyFactory.textSize(25f),
PropertyFactory.textColor(Color.parseColor("#00FF08")),
PropertyFactory.textOpacity(1f)
);
}
}
}
});
}
});
}
});
}
// Add the mapView's own lifecycle methods to the activity's lifecycle methods
@Override
public void onStart() {
super.onStart();
mapView.onStart();
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onStop() {
super.onStop();
mapView.onStop();
}
@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?