使用Mapbox GL JS表达式设置icon-offset

问题描述 投票:0回答:1

我正在尝试为名为“marker_purple”的图标设置[5,-19]的图标偏移,所有其他图标的偏移量为[0,0]。我的尝试导致给定图层没有显示图标。此图层仅包含点几何类型,并具有许多不同的图标类型。

我尝试过的:

"layout": {
  "icon-image": "{icon}",
  "icon-offset": [
    "case",
    ["==", ["get", "icon"], "marker_purple"],
    [5, -19],
    [0, 0]
  ]
}

我还尝试了以下表达式,因为表达式无法返回数组,但仍然没有图标显示。

"layout": {
   "icon-image": "{icon}",
   "icon-offset": [
      [
         "case",
         ["==", ["get", "icon"], "marker_purple"],
         5,
         0
      ],
      [
         "case",
         ["==", ["get", "icon"], "marker_purple"],
         -19,
         0
      ]
    ]  
 }

如果我指定一个没有表达式的偏移量,一切都很完美。

"icon-offset": [10,10] 

所有图标都移动了[10,10]

是否有一些我缺少的东西让表达式正常工作?

我感谢您花时间阅读本文以及您可以提供的任何帮助。

mapbox mapbox-gl-js mapbox-gl
1个回答
1
投票
"layout": {
  "icon-image": "{icon}",
  "icon-offset": [
    "case",
    ["==", ["get", "icon"], "marker_purple"],
    ["literal", [5, -19]],
    ["literal", [0, 0]]
  ]
}

数组值需要“literal”表达式运算符。

© www.soinside.com 2019 - 2024. All rights reserved.