表达式
任何layout
属性、paint
属性或filter
属性的值都可以指定为_表达式_。表达式定义了一个公式,用于使用下面描述的_运算符_计算属性的值。MapLibre提供的表达式运算符集包括:
- 数学运算符,用于对数值执行算术和其他操作
- 逻辑运算符,用于操作布尔值和进行条件决策
- 字符串运算符,用于操作字符串
- 数据运算符,提供对源要素属性的访问
- 相机运算符,提供对定义当前地图视图的参数的访问
表达式表示为JSON数组。表达式数组的第一个元素是一个字符串,命名表达式运算符,例如"*"
或"case"。后面的元素(如果有)是表达式的_参数_。每个参数要么是字面值(字符串、数字、布尔值或null
),要么是另一个表达式数组。
[expression_name, argument_0, argument_1, ...]
数据表达式
_数据表达式_是任何访问要素数据的表达式——也就是说,使用以下任何数据运算符的表达式:get、has、id、geometry-type、properties或feature-state。数据表达式允许要素的属性或状态决定其外观。它们可以用来区分同一图层内的要素并创建数据可视化。
{
"circle-color": [
"rgb",
// 当feature.properties.temperature较高时,红色值较高
["get", "temperature"],
// 绿色始终为零
0,
// 当feature.properties.temperature较低时,蓝色值较高
["-", 100, ["get", "temperature"]]
]
}
此示例使用get运算符获取每个要素的temperature
值。该值用于计算rgb运算符的参数,根据红、绿、蓝组件定义颜色。
数据表达式允许作为filter
属性的值,以及大多数paint和layout属性的值。然而,有些paint和layout属性尚不支持数据表达式。支持级别由每个属性的"SDK支持"表中的"数据驱动样式"行指示。带有feature-state运算符的数据表达式仅允许用于paint属性。
相机表达式
_相机表达式_是任何使用zoom运算符的表达式。这些表达式允许图层的外观随着地图的缩放级别而变化。相机表达式可用于创建深度的外观并控制数据密度。
{
"circle-radius": [
"interpolate", ["linear"], ["zoom"],
// 缩放级别为5(或更小) -> 圆半径为1px
5, 1,
// 缩放级别为10(或更大) -> 圆半径为5px
10, 5
]
}
此示例使用interpolate运算符,使用一组输入-输出对定义缩放级别和圆大小之间的线性关系。在这种情况下,表达式表示当缩放级别为5或以下时,圆半径应为1像素,当缩放级别为10或以上时,圆半径应为5像素。在这两个缩放级别之间,圆半径将在1和5像素之间线性插值。
相机表达式允许在任何可以使用表达式的地方使用。当相机表达式用作layout或paint属性的值时,它必须采用以下形式之一:
[ "interpolate", interpolation, ["zoom"], ... ]
或:
[ "step", ["zoom"], ... ]
或:
[
"let",
... 变量绑定...,
[ "interpolate", interpolation, ["zoom"], ... ]
]
或:
[
"let",
... 变量绑定...,
[ "step", ["zoom"], ... ]
]
也就是说,在layout或paint属性中,["zoom"]
只能作为外部interpolate或step表达式的输入出现,或者出现在let表达式内的这种表达式中。
layout和paint属性的相机表达式评估时机有一个重要区别。Paint属性的相机表达式在缩放级别变化时会重新评估,即使是小数变化。例如,当地图在缩放级别4.1和4.6之间移动时,paint属性的相机表达式将持续重新评估。_Layout属性_的相机表达式仅在整数缩放级别评估。随着缩放从4.1变化到4.6,它将_不会_被重新评估。
组合
表达式语言是基于函数的:运算符接受零个或多个表达式作为参数,并返回单个值。这种设计允许通过将一个表达式嵌入另一个表达式来构建具有任意复杂性的表达式。
{
"circle-opacity": [
"case",
["<", ["get", "population"], 100000],
1.0,
["/", ["get", "population"], 100000]
]
}
在此示例中,运算符["<", ["get", "population"], 100000]
返回一个布尔值,该值控制case运算符的选择。如果它返回true,则case返回1.0;否则,case将评估和返回表达式["/", ["get", "population"], 100000]
。
类型系统
表达式语言是动态类型的,这意味着在表达式求值期间可能出现类型错误。例如,如果输入["get", "population"]
返回字符串"高"
,则表达式["<", ["get", "population"], 100000]
将产生运行时错误。
为避免这种情况,每个运算符都会强制执行预期类型,或者在可能的情况下将值转换为预期类型。例如,如果"circle-color"
属性的值为["get", "color"]
,并且该属性的值为3
,则值将被转换为字符串"3"
。
表达式参考包括每个运算符的预期类型。如果操作符的输入与预期类型不同,则:
- 如果输入是null、数组或对象,则表达式将返回null;
- 如果输入是数字,则当预期类型为:
- string:数字将转换为字符串,例如,3->3.000
- color:数字将被解释为RGB颜色的一部分。
0
->rgba(0, 0, 0, 1)
,255
->rgb(255, 0, 0, 1) - array:输入将转换为包含等效数字的长度为1的数组
- 如果输入是布尔值,则当预期类型为:
- string:布尔值将转换为字符串,例如,true->"true"
- array:输入将转换为包含等效布尔值的长度为1的数组
- 如果输入是字符串,则当预期类型为:
- color:输入将解析为CSS颜色值,如规范所示
- array:输入将转换为包含等效字符串的长度为1的数组
对于大多数运算符,如果任何参数为null,则结果也将为null。具有此行为的所有运算符在其描述中注明了。
变量绑定
let
使用提供的变量绑定来评估内部表达式;
["let", name1, value1, name2, value2, ... nameN, valueN, expression]
创建带有指定名称和值的变量绑定,并在评估子expression
时使用这些绑定,这些变量仅在子expression
中可见;
nameN
: stringvalueN
: anyexpression
: any
示例:
"circle-radius": [
"let",
"radius", 5,
"stroke", 1,
["+", ["var", "radius"], ["var", "stroke"]]
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
var
引用之前通过let表达式创建的变量;
["var", name]: any
name
: string
示例:
"circle-radius": [
"let",
"radius", 5,
["+", ["var", "radius"], 2]
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
类型
literal
提供JavaScript风格的字面值数组或对象;
["literal", value]: value
value
: array 或 object
示例:
"circle-stroke-dasharray": ["literal", [1, 2]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
array
断言输入为数组(可选地指定期望的项目类型);
如果提供了一个itemType
,则表达式为各项目类型强制转换的副本;
如果提供了N
,则表达式确保输入数组长度为N
。如果输入数组长度小于N
,则表达式将添加在最后位置类型为itemType
的默认值;如果输入数组长度大于N
,则表达式将丢弃额外的值;
如果输入不是数组,则表达式将返回空数组;
["array", itemType?, N?]: array<itemType, N>
itemType
: string,类型之一:string
、number
、boolean
N
: number
示例:
"circle-stroke-dasharray": ["array", "number", 2]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
typeof
返回输入值的类型;
["typeof", input]: string
input
: any
示例:
"circle-color": [
"match",
["typeof", ["get", "attr"]],
"number", "red",
"string", "blue",
"boolean", "green",
"black"
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
string
断言输入为字符串。如果输入为null或具有定义toString
方法的值,则表达式将评估为包含toString()
方法输出的字符串值;
["string", input]: string
input
: any
示例:
"text-field": ["string", ["get", "name"]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
number
断言输入为数字。如果输入为null或false,则表达式将评估为0;如果输入为true,则表达式将评估为1;如果输入为字符串,则表达式将尝试将其解析为数字;否则,表达式将评估为0;
["number", input, fallback?]: number
input
: anyfallback
: number
示例:
"circle-radius": ["number", ["get", "a-string-value"], 0]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
boolean
断言输入为布尔值。如果输入为0或""
或null或false,则表达式将评估为false;否则,表达式将评估为true;
["boolean", input]: boolean
input
: any
示例:
"fill-enabled": ["boolean", ["get", "fill"]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
object
断言输入为对象;
["object", input]: object
input
: any
示例:
"icon-image": ["object", ["get", "icon"]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
collator
返回用于区域特定字符串比较的Collator
对象;
["collator", caseSensitive, diacriticSensitive, locale]: collator
caseSensitive
: boolean,可选,默认为falsediacriticSensitive
: boolean,可选,默认为falselocale
: string,可选,默认为系统区域设置
示例:
"text-field": [
"format",
["resolved-locale", ["collator", {"case-sensitive": false, "diacritic-sensitive": false, "locale": "en"}]]
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.45.0 | 6.5.0 | 4.2.0 |
format
创建一个FormattedString
,用于文本格式设置。如果给定的每个部分的image
选项为true,则尝试将该部分的文本解析为图标名称,如果有效,则将返回对应于图标名称的图标;
["format", text, options?, ..., text, options?]: formatted
text
: string 或 formattedoptions
: object,可选:"text-font"
: array<string>"font-scale"
: number"text-color"
: color"image"
: boolean
示例:
"text-field": ["format", "foo", {"font-scale": 1.2}, "bar", {"text-color": "blue"}]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.48.0 | 7.0.0 | 4.7.0 |
图像支持 | 0.49.0 | 8.1.0 | 5.0.0 |
image
返回与图像名称对应的图像。返回的值只能用于图标相关表达式,如icon-image
。如果图像名称无效,则返回null;
["image", input]: resolvedImage
input
: string 或 resolvedImage
示例:
"icon-image": ["image", ["concat", ["get", "image_name"], "-full"]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.52.0 | 8.1.0 | 5.0.0 |
number-format
根据给定的参数将数字格式化为字符串;
["number-format", input, options?]: string
input
: numberoptions
: object,可选:"locale"
: string"currency"
: string"min-fraction-digits"
: number"max-fraction-digits"
: number
示例:
"text-field": ["number-format", ["get", "population"], {"currency": "USD", "min-fraction-digits": 2, "max-fraction-digits": 2}]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.54.0 | 8.3.0 | 5.2.0 |
to-string
将输入值转换为字符串。如果输入是null,则表达式返回"";
["to-string", input]: string
input
: any
示例:
"text-field": ["to-string", ["get", "population"]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
to-number
尝试将输入值转换为数字。如果输入是null或false,则返回0;如果输入是true,则返回1;如果输入是字符串,则尝试将其解析为数字。如果字符串不能解析为数字,则返回fallback,如果没有指定,则返回0;
["to-number", input, fallback?]: number
input
: anyfallback
: number
示例:
"symbol-sort-key": ["to-number", ["get", "id"], 0]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
to-boolean
将输入值转换为布尔值。如果输入是0或""
或null或false,则结果为false;否则,结果为true;
["to-boolean", input]: boolean
input
: any
示例:
"fill-enabled": ["to-boolean", ["get", "fill"]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
to-color
尝试将输入值转换为颜色。如果输入是有效的颜色表示,则返回该颜色;如果输入是数字,则将其解释为RGB颜色的一部分,以3-/4-元素数组的形式;否则,返回默认的黑色颜色;
["to-color", input]: color
input
: any
示例:
"circle-color": ["to-color", ["get", "color"]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
查找
at
在指定索引处检索数组中的项。如果索引<0或>=数组长度,则返回回退值(如果提供)或null;
["at", number, array, fallback?]: ItemType
number
: numberarray
: arrayfallback
: any,可选
示例:
"text-field": ["at", 0, ["get", "name"]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.53.0 | 8.3.0 | 5.2.0 |
in
确定项目是否存在于数组或字符串中。如果输入是数组,则该表达式检查该数组是否包含输入作为其元素之一;如果输入是字符串,则该表达式检查该字符串是否包含输入作为子字符串;
["in", InputType, array]: boolean
InputType
: string | number | booleanarray
: array | string
示例:
"filter": ["in", ["get", "class"], "street_limited", "street_regular"]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
index-of
返回在输入
中找到的第一个needle
的索引位置,如果找不到needle
,则返回-1;
如果输入是数组,则该表达式返回该数组中第一个等于needle的元素的索引,如果指定了start
,则从该索引处或之后开始搜索;
如果输入是字符串,则该表达式返回子字符串匹配的第一个位置,如果指定了start
,则从该位置或之后开始搜索;
["index-of", needle, input, start?]: number
needle
: string | number | booleaninput
: array | stringstart
: number,可选,默认为0
示例:
"text-field": ["index-of", "林", ["get", "name"]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.53.0 | 8.3.0 | 5.2.0 |
slice
从数组或字符串中返回一个部分;
如果输入是数组,则该表达式返回该数组的开始
索引处(包括)到结束
索引处(不包括)的项目子集;
如果输入是字符串,则该表达式返回该字符串的开始
索引处(包括)到结束
索引处(不包括)的子字符串;
如果开始<0或结束<0,则将索引从数组或字符串的末尾开始计算;
["slice", input, start, end?]: InputType
input
: array | stringstart
: numberend
: number,可选,默认为输入长度
示例:
"text-field": ["slice", ["get", "name"], 0, 5]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.53.0 | 8.3.0 | 5.2.0 |
global-state
获取全局状态变量的当前值;
["global-state", name, fallback]: any
name
: stringfallback
: any
示例:
"text-field": ["global-state", "state-variable", "defaultValue"]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | ❌ (#1698) | ❌ (#1698) | ❌ (#1698) |
get
获取当前要素的属性值。如果指定的属性缺失或其值为null或undefined,则返回回退值(如果提供)或null;
["get", property, object?]: any
property
: stringobject
: object,可选,默认为当前要素的properties
示例:
"text-color": ["get", "color", ["properties"]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
has
检查对象是否包含指定的属性;
["has", property, object?]: boolean
property
: stringobject
: object,可选,默认为当前要素的properties
示例:
"text-color": ["has", "color", ["properties"]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
length
获取字符串或数组的长度;
["length", string | array | formatted]: number
string
: stringarray
: arrayformatted
: formatted
示例:
"text-field": ["length", ["get", "name"]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
决策
case
根据条件分支选择输出。对每个输入对进行评估,如果第一个表达式评估为真,则输出相应的结果。如果所有条件都为假,则返回默认结果;
["case", condition1, output1, ..., conditionN, outputN, default]: OutputType
condition
: booleanoutput
: OutputTypedefault
: OutputType
示例:
"circle-color": [
"case",
["<", ["get", "population"], 500000], "green",
["<", ["get", "population"], 1000000], "blue",
"red"
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
match
根据输入值从标签列表中选择一个输出。如果找不到匹配项,则输出默认值;
["match", input, label1, output1, ..., labelN, outputN, default]: OutputType
input
: number | stringlabel
: number | string | array<number | string>output
: OutputTypedefault
: OutputType
示例:
"circle-color": [
"match",
["get", "type"],
"residential", "green",
"commercial", "blue",
"red"
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
coalesce
评估每个表达式,直到获得第一个非空值,然后返回该值;
["coalesce", value1, value2, ..., valueN]: FirstNotNullType
value
: any
示例:
"text-field": ["coalesce", ["get", "name_zh"], ["get", "name_en"]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
==
测试两个值是否相等。当两个操作数都是空值时返回true;当一个操作数为空值但另一个不是时返回false;当两个操作数都是对象时,返回"true"仅当两个对象引用相同的对象时;
["==", value1, value2]: boolean
value1
: anyvalue2
: any
示例:
"circle-color": [
"==",
["get", "type"],
"commercial"
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
!=
测试两个值是否不相等。当两个操作数都是空值时返回false;当一个操作数为空值但另一个不是时返回true;当两个操作数都是对象时,返回"false"仅当两个对象引用相同的对象时;
["!=", value1, value2]: boolean
value1
: anyvalue2
: any
示例:
"circle-color": [
"!=",
["get", "type"],
"commercial"
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
>
测试第一个输入是否严格大于第二个;
[">", value1, value2]: boolean
value1
: numbervalue2
: number
示例:
"circle-radius": [
">",
["get", "population"],
100000
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
<
测试第一个输入是否严格小于第二个;
["<", value1, value2]: boolean
value1
: numbervalue2
: number
示例:
"circle-radius": [
"<",
["get", "population"],
100000
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
>=
测试第一个输入是否大于或等于第二个;
[">=", value1, value2]: boolean
value1
: numbervalue2
: number
示例:
"circle-radius": [
">=",
["get", "population"],
100000
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
<=
测试第一个输入是否小于或等于第二个;
["<=", value1, value2]: boolean
value1
: numbervalue2
: number
示例:
"circle-radius": [
"<=",
["get", "population"],
100000
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
all
对所有输入进行逻辑"与"操作,如果所有输入都为true,则返回true;如果任何输入为false,则返回false。对于空输入,返回true;
["all", value1, value2, ...]: boolean
value
: boolean
示例:
"circle-color": [
"all",
[">=", ["get", "population"], 100000],
["<", ["get", "population"], 500000]
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
any
对所有输入进行逻辑"或"操作,如果任何输入为true,则返回true;如果所有输入都为false,则返回false。对于空输入,返回false;
["any", value1, value2, ...]: boolean
value
: boolean
示例:
"circle-color": [
"any",
["==", ["get", "type"], "commercial"],
["==", ["get", "type"], "residential"]
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
!
对输入进行逻辑否定。如果输入为false,则返回true;如果输入为true,则返回false;
["!", value]: boolean
value
: boolean
示例:
"circle-color": [
"!",
["==", ["get", "type"], "commercial"]
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
within
测试给定的几何体是否在当前特征内部。该几何体必须是GeoJSON多边形几何体;
["within", geometry]: boolean
geometry
: object
示例:
"fill-color": [
"within",
{
"type": "Polygon",
"coordinates": [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]]
}
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.53.0 | 9.1.0 | 5.9.0 |
坡度、尺度、曲线
step
通过评估一系列输入和输出的离散分段函数来产生阶梯状的结果;
输入可以是具有连续数值的任何表达式(例如,["get", "population"]
或 ["zoom"]
);
停止点定义了输入值的边界,输出值则是输入值落在相应停止点内时应用的值;
如果输入小于第一个停止,则表达式返回第一个输出;
如果输入等于或大于最后一个停止,则表达式返回最后一个输出;
停止必须按照数值递增的顺序排序:第一个值表示输入的下限,然后每对值表示下一个步进的输入上限(排他)和输出;
["step", input, base, stop_input_1, stop_output_1, stop_input_n, stop_output_n, ...]: OutputType
input
: numberbase
: OutputTypestop_input_n
: number,必须按递增顺序排列stop_output_n
: OutputType
示例:
"circle-color": [
"step",
["get", "population"],
"blue",
100000, "green",
500000, "red"
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.43.0 | 6.0.0 | 4.0.0 |
interpolate
使用给定的插值方法,在输入和输出停止点之间线性插值产生连续、平滑的结果;
输入可以是具有连续数值的任何表达式(例如,["get", "population"]
或 ["zoom"]
);
["interpolate", interpolation, input, stop_input_1, stop_output_1, stop_input_n, stop_output_n, ...]: OutputType
interpolation
: ["linear"] | ["exponential", base] | ["cubic-bezier", x1, y1, x2, y2],插值类型input
: numberstop_input_n
: number,必须按递增顺序排列stop_output_n
: OutputType,OutputType可以是数字、数组<数字>(长度为2-4的)或颜色
["linear"]: 在停止之间线性插值
["exponential", base]: 使用指定基数在停止之间指数插值
["cubic-bezier", x1, y1, x2, y2]: 使用指定的立方贝塞尔曲线插值
示例:
"circle-radius": [
"interpolate", ["linear"], ["zoom"],
12, 1,
22, 5
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
interpolate-hcl
使用给定的插值方法,在输入和输出停止点之间使用HCL颜色空间进行插值产生连续、平滑的结果;
输入可以是具有连续数值的任何表达式(例如,["get", "population"]
或 ["zoom"]
);
["interpolate-hcl", interpolation, input, stop_input_1, stop_output_1, stop_input_n, stop_output_n, ...]: OutputType
interpolation
: ["linear"] | ["exponential", base] | ["cubic-bezier", x1, y1, x2, y2],插值类型input
: numberstop_input_n
: number,必须按递增顺序排列stop_output_n
: color
["linear"]: 在停止之间线性插值
["exponential", base]: 使用指定基数在停止之间指数插值
["cubic-bezier", x1, y1, x2, y2]: 使用指定的立方贝塞尔曲线插值
示例:
"circle-color": [
"interpolate-hcl", ["linear"], ["zoom"],
12, "blue",
22, "green"
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.49.0 | 8.3.0 | 5.3.0 |
interpolate-lab
使用给定的插值方法,在输入和输出停止点之间使用LAB颜色空间进行插值产生连续、平滑的结果;
输入可以是具有连续数值的任何表达式(例如,["get", "population"]
或 ["zoom"]
);
["interpolate-lab", interpolation, input, stop_input_1, stop_output_1, stop_input_n, stop_output_n, ...]: OutputType
interpolation
: ["linear"] | ["exponential", base] | ["cubic-bezier", x1, y1, x2, y2],插值类型input
: numberstop_input_n
: number,必须按递增顺序排列stop_output_n
: color
["linear"]: 在停止之间线性插值
["exponential", base]: 使用指定基数在停止之间指数插值
["cubic-bezier", x1, y1, x2, y2]: 使用指定的立方贝塞尔曲线插值
示例:
"circle-color": [
"interpolate-lab", ["linear"], ["zoom"],
12, "blue",
22, "green"
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.49.0 | 8.3.0 | 5.3.0 |
数学
ln2
自然对数的底 e 的 ln(2),大约为 0.693;
["ln2"]: number
示例:
"circle-radius": ["*", ["ln2"], 2]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
pi
数学常数 π,大约为 3.14159;
["pi"]: number
示例:
"circle-radius": ["*", ["pi"], 2]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
e
自然对数的底数,大约为 2.71828;
["e"]: number
示例:
"circle-radius": ["*", ["e"], 2]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
+
对给定的一组或多组数字求和;
["+", number, number, ...]: number
number
: number
示例:
"circle-radius": ["+", 5, 5]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
*
对给定的一组或多组数字求积;
["*", number, number, ...]: number
number
: number
示例:
"circle-radius": ["*", 5, 2]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
-
对两个数字做减法,或对一个数字取负号;
["-", number, number?]: number
number
: number
示例:
"circle-radius": ["-", 5, 3]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
/
对两个数字做除法;
["/", number, number]: number
number
: number
示例:
"circle-radius": ["/", 10, 2]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
%
求两个数字相除的余数;
["%", number, number]: number
number
: number
示例:
"circle-radius": ["%", 10, 3]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
^
求底数的指定幂次方;
["^", number, number]: number
number
: number
示例:
"circle-radius": ["^", 2, 3]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
sqrt
计算数字的平方根;
["sqrt", number]: number
number
: number
示例:
"circle-radius": ["sqrt", 4]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.42.0 | 6.0.0 | 4.0.0 |
log10
计算以10为底的对数;
["log10", number]: number
number
: number
示例:
"circle-radius": ["log10", 100]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
ln
计算自然对数;
["ln", number]: number
number
: number
示例:
"circle-radius": ["ln", 2]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
log2
计算以2为底的对数;
["log2", number]: number
number
: number
示例:
"circle-radius": ["log2", 8]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
sin
计算正弦值;
["sin", number]: number
number
: number
示例:
"circle-radius": ["sin", 0]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
cos
计算余弦值;
["cos", number]: number
number
: number
示例:
"circle-radius": ["cos", 0]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
tan
计算正切值;
["tan", number]: number
number
: number
示例:
"circle-radius": ["tan", 0]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
asin
计算反正弦值;
["asin", number]: number
number
: number
示例:
"circle-radius": ["asin", 0]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
acos
计算反余弦值;
["acos", number]: number
number
: number
示例:
"circle-radius": ["acos", 1]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
atan
计算反正切值;
["atan", number]: number
number
: number
示例:
"circle-radius": ["atan", 0]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
min
返回一组数字中的最小值;
["min", number, number, ...]: number
number
: number
示例:
"circle-radius": ["min", 5, 10]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
max
返回一组数字中的最大值;
["max", number, number, ...]: number
number
: number
示例:
"circle-radius": ["max", 5, 10]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
round
将数字四舍五入到最接近的整数;
["round", number]: number
number
: number
示例:
"circle-radius": ["round", 5.2]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.45.0 | 6.0.0 | 4.0.0 |
abs
计算数字的绝对值;
["abs", number]: number
number
: number
示例:
"circle-radius": ["abs", -5]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.45.0 | 6.0.0 | 4.0.0 |
ceil
将数字向上舍入到最接近的整数;
["ceil", number]: number
number
: number
示例:
"circle-radius": ["ceil", 5.2]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.45.0 | 6.0.0 | 4.0.0 |
floor
将数字向下舍入到最接近的整数;
["floor", number]: number
number
: number
示例:
"circle-radius": ["floor", 5.7]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.45.0 | 6.0.0 | 4.0.0 |
distance
计算两个坐标之间的距离;
["distance", point1, point2]: number
point1
: array<number, 2>point2
: array<number, 2>
示例:
"circle-radius": ["distance", [-73.9876, 40.7661], ["get", "location"]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.54.0 | 9.1.0 | 5.8.0 |
颜色
to-rgba
返回代表输入颜色的四元组,包含r, g, b, a组件,各自范围为[0, 255]中的整数和[0, 1]中的浮点数;
["to-rgba", color]: array<number, 4>
color
: color
示例:
"circle-color": ["to-rgba", ["get", "color"]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
rgb
从红、绿、蓝分量(各自范围为0-255)创建一个颜色值;
["rgb", number, number, number]: color
number
: number
示例:
"circle-color": ["rgb", 255, 0, 0]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
rgba
从红、绿、蓝分量(各自范围为0-255)和alpha分量(范围为0-1)创建一个颜色值;
["rgba", number, number, number, number]: color
number
: number
示例:
"circle-color": ["rgba", 255, 0, 0, 1]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
要素数据
properties
获取当前要素的属性对象;
["properties"]: object
示例:
"icon-image": ["concat", ["get", "icon"], "-", ["get", "type", ["properties"]]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
feature-state
获取当前要素的要素状态值,如果该状态未设置,则返回undefined;
["feature-state", string]: any
string
: string
示例:
"fill-color": ["feature-state", "hover"]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.46.0 | 7.4.0 | 4.11.0 |
geometry-type
返回要素的简单几何类型:Point
、LineString
或Polygon
。MultiPoint
、MultiLineString
和MultiPolygon
分别作为Point
、LineString
和Polygon
返回;
["geometry-type"]: string
示例:
"some-property": ["==", ["geometry-type"], "Polygon"]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
id
获取要素的id(如果有);
["id"]: any
示例:
"some-property": ["id"]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
line-progress
获取沿渐变线的进度。只能在line-gradient
属性中使用;
["line-progress"]: number
示例:
"some-property": ["line-progress"]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.45.0 | 6.5.0 | 4.6.0 |
accumulated
获取到目前为止累积的聚类属性值。只能在聚类GeoJSON源的clusterProperties
选项中使用;
["accumulated"]: any
示例:
"some-property": ["accumulated"]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.53.0 | ✅ | ✅ |
缩放
zoom
获取当前缩放级别。注意,在样式布局和绘制属性中,["zoom"]
只能作为顶层"step"或"interpolate"表达式的输入出现;
["zoom"]: number
示例:
"some-property": [
"interpolate",
["linear"],
["zoom"],
15,
0,
15.05,
["get", "height"]
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
热力图
heatmap-density
获取热力图层中像素的核密度估计,这是对特定像素周围数据点拥挤程度的相对度量。只能在heatmap-color
属性中使用;
["heatmap-density"]: number
示例:
"some-property": ["heatmap-density"]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
高程浮雕
elevation
从raster-dem
源获取像素的海拔高度(以米为单位,高于raster-dem
瓦片的垂直基准面)。只能在color-relief
层的color-relief-color
属性中使用;
["elevation"]: number
示例:
"some-property": ["elevation"]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | ❌ (#5666) | ❌ (#3408) | ❌ (#3408) |
字符串
is-supported-script
如果输入字符串预计能够清晰渲染,则返回true
。如果输入字符串包含可能无法在不丢失含义的情况下呈现的部分(例如,需要复杂文本整形的印度文字,或者在MapLibre GL JS中未使用mapbox-gl-rtl-text
插件的从右到左的文字),则返回false
;
["is-supported-script", input]: boolean
input
: string
示例:
"some-property": ["is-supported-script", "दिल्ली"]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.45.0 | 6.6.0 | ✅ |
upcase
返回转换为大写的输入字符串。遵循Unicode默认大小写转换算法和Unicode字符数据库中的与区域设置无关的大小写映射;
- 更改标签的大小写
["upcase", input]: string
input
: string
示例:
"some-property": ["upcase", ["get", "name"]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
downcase
返回转换为小写的输入字符串。遵循Unicode默认大小写转换算法和Unicode字符数据库中的与区域设置无关的大小写映射;
- 更改标签的大小写
["downcase", input]: string
input
: string
示例:
"some-property": ["downcase", ["get", "name"]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
concat
返回由输入连接组成的string
。每个输入都像to-string
一样转换为字符串;
- 向地图添加生成的图标
- 创建时间滑块
- 使用回退图像
- 可变标签放置
["concat", input_1, ..., input_n]: string
input_i
: any
示例:
"some-property": ["concat", "square-rgb-", ["get", "color"]]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.41.0 | 6.0.0 | 4.0.0 |
resolved-locale
返回提供的collator
使用的区域设置的IETF语言标签。这可用于确定默认系统区域设置,或确定是否成功加载了请求的区域设置;
["resolved-locale", collator]: string
collator
: collator
示例:
"some-property": [
"resolved-locale",
[
"collator",
{
"case-sensitive": true,
"diacritic-sensitive": false,
"locale": "de"
}
]
]
SDK支持 | MapLibre GL JS | MapLibre Native Android | MapLibre Native iOS |
---|---|---|---|
基本功能 | 0.45.0 | 6.5.0 | 4.2.0 |