MapsGL Android - Adjusting temperature draw range in real time
A draw range limits which values a sample layer paints at all. Values outside it are left transparent, not clamped to the end colours of the scale, so narrowing it isolates a band of interest — only freezing temperatures, say — without touching the colour scale. This example sets the range for the temperatures layer from two sliders.
It is the Android port of the MapsGL JS Adjusting temperature draw range in real time example. The complete activity ships in the MapsGL Android SDK demo as AdjustDrawRangeActivity.

Set the range in the data’s units
The range is sample.drawRange on the layer’s paint:
val config = WeatherService.Temperatures(controller.service)
val low = minOf(lowerF, upperF)
val high = maxOf(lowerF, upperF)
(config.layer.paint as SampleLayerPaint).sample.drawRange = celsius(low)..celsius(high)
controller.addWeatherLayer(config)The sliders read in Fahrenheit because that is what the map displays, but the range is set in Celsius. A draw range is always in the data’s own units, and every MapsGL dataset is metric.
Both sliders span the same fixed −60 °F to 120 °F track. Limiting one by the other’s current value would rescale its track whenever the other moved, so the same thumb position would mean a different temperature from one drag to the next. The cost is that the two can be dragged past each other, which is why the pair is ordered with minOf and maxOf rather than trusted.
Re-add the layer rather than update it
The JS example calls setPaintProperty('sample.drawRange.min', …) on each slider move. The Android demo removes the temperatures layer and adds it again with the new range:
private fun reAddTemperatures() = reAddLayer(LayerCode.TEMPERATURES) { addTemperatures(controller) }Paint is captured when the layer is built, so changing drawRange on a layer that is already on the map has no effect on its own — and setPaintProperty does not take a draw range path; on sample layers it handles sample.colorscale. Re-adding always picks up the new value.
Re-adding is not free: the layer re-registers and re-reads its cached tiles. So the sliders update their labels as they move, and only apply the range when the finger lifts.
Leaving one end open
Both ends are set here because both sliders are. To clip only one side, use BoundedRange from com.xweather.mapsgl.types.math_type:
paint.sample.drawRange = BoundedRange.atMost(0.0) // freezing and below
paint.sample.drawRange = BoundedRange.atLeast(32.2) // 90 °F and aboveThe open end is filled in from the layer’s own data range, which is the counterpart of setting only min or only max in JS. A plain ClosedRange can do the same only by naming an arbitrary floor or ceiling.
Related
- Create a freeze layer — a draw range with an open bottom, combined with a stepped colour scale
- Customizing temperature colors — changing the colour scale of the same layer rather than the part of it that draws