经纬志
首页 / MapLibre / API 文档

AttributionControl

先看结论

右下角的来源声明控件,汇总各个数据源的 attribution,并允许追加自定义版权文字。

最后验证 2026-11-03 · MapLibre GL JS 4.x · 更新 2026-10-28 · 引用本文
map.addControl(new maplibregl.AttributionControl({ compact: false, customAttribution: '…' }))

参数

字段默认说明
compacttrue折叠成一个 ⓘ 按钮,点开显示全文
customAttributionMapLibre 官方链接字符串或字符串数组,追加到来源声明后

控件默认位置是右下角。地图创建时 attributionControl 传入对象即为该控件的参数; 传 false 则不自动添加,之后可以手动 addControl。

成员

成员说明
onAdd(map)挂载时调用,返回控件容器
onRemove(map)移除时调用,清理 DOM 与监听
getDefaultPosition()默认停靠位置,值为 'bottom-right'

示例

var map = new maplibregl.Map({
  container: 'map',
  style: 'https://demotiles.maplibre.org/style.json',
  center: [104.2, 35.5],
  zoom: 3,
  attributionControl: false
});

map.addControl(new maplibregl.AttributionControl({
  compact: false,
  customAttribution: [
    '© OpenStreetMap',
    '数据整理:内部专题库'
  ]
}), 'bottom-right');

注意事项

每个 source 的 attribution 字段会自动汇总进控件, 同一段文字出现多次会被去重。切换底图后旧来源会自动消失, 不需要手工维护。

compact: true 时折叠按钮在触屏上不易点中, 面向移动端的页面通常设为 false,或者把控件移到 'bottom-left' 避开右下角的手势区域。

内容会被当作 HTML 解析。customAttribution 支持链接标签,因此不要拼接来源不明的字符串;外部输入需要先转义。

引用本文

经纬志(2026)。《AttributionControl》。https://www.jingweizhi.com/maplibre/api/attribution-control.html(访问日期 2026-11-03)。