Skip to Content
ExamplesMap Units

MapsGL Android - Setting map units

This example shows temperatures as text labels over Seattle, in Celsius, because the map’s units say so. The units are set once on the controller before the layer is added; everything drawn afterwards formats against them.

It is the Android port of the MapsGL JS Setting map units example. The complete activity ships in the MapsGL Android SDK demo  as SetMapUnitsActivity.

Temperature values as text labels across the Seattle area, with no basemap labels

Units live on the controller

Where JS passes a units object to the controller’s constructor, Android assigns MapboxMapController.units, a MeasurementUnits value:

controller.units = MeasurementUnits.IMPERIAL.copy( temperature = UnitTemperature.celsius, speed = UnitSpeed.kilometersPerHour, distance = UnitLength.kilometers, pressure = UnitPressure.millibars, ) controller.addWeatherLayer(LayerCode.TEMPERATURES_TEXT)

Starting from IMPERIAL and copying in four units mirrors the JS object, which names only the categories it changes and leaves the rest at their defaults. MeasurementUnits.METRIC would switch every category at once instead.

The units are assigned before the layer is added, so the first labels are already in Celsius. Assigning them later works too — the labels reformat without re-sampling — which is what Change map units shows.

Labels are numbers alone

Like MapsGL JS, the text layer draws the value by itself — 14, not 14°C — and leaves the unit to the legend. The example adds a Show units switch, not in the JS version, bound to showTextLabelUnits:

addToggle("Show units", controller.showTextLabelUnits) { on -> controller.showTextLabelUnits = on }

Turned on, every label carries its unit, and the labels already on the map reformat in place. That is worth having on a phone, where the label may be all a reader sees.

Hiding the basemap’s labels

The JS example hides every symbol layer in its Mapbox style, so the temperatures are the only text on the map. The same loop is here for classic styles, where labels are top-level layers:

style.styleLayers .filter { it.type == "symbol" } .forEach { style.setStyleLayerProperty(it.id, "visibility", Value("none")) }

This demo’s basemap is Mapbox Standard, though, and its labels are not top-level layers. They belong to the style’s basemap import and are switched off through its configuration:

listOf("showPlaceLabels", "showRoadLabels", "showPointOfInterestLabels", "showTransitLabels") .forEach { key -> style.setStyleImportConfigProperty("basemap", key, Value.valueOf(false)) }

On a classic style there is no such import, and the call simply returns an error that the example ignores.

  • Change map units — switching units at runtime, with the legend relabelling to match
© 2026 Xweather (opens in a new tab)Terms of Service (opens in a new tab)Privacy Policy (opens in a new tab)