javascript - mapbox-gl js 对 "text-offset"使用步进斜坡

标签 javascript mapbox mapbox-gl-js mapbox-gl mapbox-marker

我在 layout 的 mapbox gl 层中使用-block 字段 text-offset

layout: { // Working
  'text-field': '{point_count_abbreviated}',
  'text-size': ['step', ['get', 'point_count'], 18, 10, 14, 100, 12],
  'text-offset': [-0.84, 0.23],
}

这按预期工作,但现在我想根据属性更改偏移量。这对于 'text-size' 非常有效,但是对于 text-offset 我找不到正确的语法。我尝试了以下方法:

layout: { // NOT working
  'text-field': '{point_count_abbreviated}',
  'text-size': ['step', ['get', 'point_count'], 18, 10, 14, 100, 12],
  'text-offset': [
    // [-0.84, 0.23],
    ['step', ['get', 'point_count'], -0.84, 10, -0.94, 100, -0.99],
    ['step', ['get', 'point_count'], 0.23, 10, 0.25, 100, 0.28],
  ],
},

也许 mapbox-gl 目前不支持文本偏移处的步进斜坡?

错误信息:

Error: layers.cluster-offline.layout.text-offset[0]: number expected, array found


layout: { // NOT working
  'text-field': '{point_count_abbreviated}',
  'text-size': ['step', ['get', 'point_count'], 18, 10, 14, 100, 12],
  'text-offset': [
    // [-0.84, 0.23],
    ['literal', 
       ['step', ['get', 'point_count'], -0.84, 10, -0.94, 100, -0.99], 
       ['step', ['get', 'point_count'], 0.23, 10, 0.25, 100, 0.28]
    ],
  ],
},

错误信息:

Error: layers.cluster-offline.layout.text-offset: array length 2 expected, length 1 found


layout: { // NOT working
  'text-field': '{point_count_abbreviated}',
  'text-size': ['step', ['get', 'point_count'], 18, 10, 14, 100, 12],
  'text-offset': [
    ['literal', ['step', ['get', 'point_count'], -0.84, 10, -0.94, 100, -0.99]], 
    ['literal', ['step', ['get', 'point_count'], 0.23, 10, 0.25, 100, 0.28]],
  ],
},

错误信息:

Error: layers.cluster-offline.layout.text-offset[0]: number expected, array found

最佳答案

您需要使用文字类型转换器包装您的文本偏移值:

'text-offset': [
  'step',                         // Expression type (discrete matching)
  ['get', 'point_count'],         // Variable to compare to
  ['literal', [-0.84, 0.23]],     // Default value (if none of the following match)
  10, ['literal', [-0.94, 0.25]], // if point_count === 10: [-0.94, 0.25]
  100, ['literal', [-0.99, 0.28]] // if point_count === 100: [-0.94, 0.28]
]

Stop output values must be literal values (i.e. not functions or expressions)

source

这里,[-0.84, 0.23] 子表达式对于 Mapbox 可能有歧义,因此您需要明确地告诉它们的类型。

关于javascript - mapbox-gl js 对 "text-offset"使用步进斜坡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51361544/

相关文章:

javascript - 计算移动框的距离以移除交点

javascript - HTML5中如何在没有任何 <input> 标签的情况下读取本地系统?

mapbox - 如何在嵌套属性上使用 mapbox "case"表达式?

javascript - 嵌入式 GeoJSON 样式是否适用于 Mapbox GL?

javascript - 在 map 加载后切换 Mapbox GL JS map 交互性

typescript - 找不到模块 'mapbox-gl/dist/mapbox-gl.js'

javascript - 是什么导致了 "jqGrid is not a function "错误?

javascript - 没有任何服务器的 WebRTC 甚至连信令服务器都不可能吗?

twitter-bootstrap-3 - 模态中的传单和 Mapbox 无法正确显示

svg - 如何将 SVG 图标添加到我的 Mapbox Studio 样式中