1

How can I pass a dynamic list of stop parameter to line Gradient function in Mapbox SDK for Android (kotlin)

Something like:

val stops = [stop1, stop2...stopN]
style.addLayer(new LineLayer("linelayer", "line-source").withProperties(
    lineCap(Property.LINE_CAP_ROUND),
    lineJoin(Property.LINE_JOIN_ROUND),
    lineWidth(14f),
    lineGradient(interpolate(
    linear(), lineProgress(),
    stops //<- How to pass a dynamic array of stops here !!!
    ))));

Is this possible?

Ngoc Phung
  • 13
  • 2

1 Answers1

1

The way to do that is to build an Expression using an ExpressionBuilder:

style.addLayer(new LineLayer("linelayer", "line-source").withProperties(
        lineCap(Property.LINE_CAP_ROUND),
        lineJoin(Property.LINE_JOIN_ROUND),
        lineWidth(14f),
        lineGradient(getGradient(progressPoints, colorsArray));
    
private fun getGradient(values:MutableList<Double>, colors:MutableList<Color>): Expression {

    val lit = Expression
        .ExpressionBuilder("interpolate")
        .addArgument(linear())

    lit.lineProgress()

    for ((i, v) in values.withIndex()) {
        lit.stop {
            // Here add your own stops and expressions
            literal(v)
            rgb(
                colors[i].red().toDouble(),
                colors[i].green().toDouble(),
                colors[i].blue().toDouble()
            )
        }
    }

    return lit.build()
}
Etherlind
  • 311
  • 2
  • 8