jk's notes
  • 加载FeatureLayer时指定字体

加载FeatureLayer时指定字体

使用 FeatureLayer 加载 FeatureServer 时可能会缺少字体报错:

image-20231122195834110

需要设置 Font, 而官方文档默认使用 js 语法: https://developers.arcgis.com/javascript/latest/api-reference/esri-symbols-Font.html

image-20231122200407411

使用 TS 很多语法不支持.

  • JS 语法使用 JSON 来描述对象, 使用 type 来描述类型, 然后使用构造函数来实现转换.
  • 而 TS 语法的 type 属性均是只读的, 需要指定准确的类型, 配置准确的属性来创建.

通过查阅文档:

  • 查询 FeatureLayer 文档, 需要设置 TextSymbol, 即设置 labelingInfo 属性. 而该属性又是 LabelClass[] 类型.
  • LabelClass 有个 symbol 属性, 可以接收 TextSymbol 类型.
  • 而 TextSymbol 有一个 Font 类型.

即代码结构为:

const featureLayer = new FeatureLayer({
  labelingInfo: [
    new LabelClass({
      symbol: new TextSymbol({
        font: new Font({
          family: 'sans-serif'
        })
      })
    })
  ]
})

再调试就没有 bug 了.

image-20231122201844177

Last Updated:
Contributors: jk