如何在 Mapbox 中绘制带动态停止的渐变线

How to draw line gradient with dynamic stop in Mapbox

如何将停止参数的动态列表传递给 Mapbox SDK 中的线渐变函数 Android (kotlin)

类似于:

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 !!!
    ))));

这可能吗?

方法是使用 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()
}