Appearance
Sources 数据源简介
数据源(Source)用于定义地图数据的格式和位置。添加数据源后,必须通过 Layers 引用它才能在地图上显示。
Sources 数据源详解
sources 对象定义了地图数据的来源。图层(Layers)通过引用数据源的 key 来渲染内容。
GeoJSON Source
type: 'geojson'
最常用的数据源,用于加载本地或远程的 GeoJSON 数据(点、线、面)。
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|---|---|---|---|---|
| data | GeoJSON 数据。可以是 URL 字符串,也可以是直接的 GeoJSON 对象。 | String, Object | - | - |
| maxzoom | 索引生成的最大缩放级别。超过此级别的数据将不再进行切片优化。 | Number | 0 ~ 24 | 18 |
| attribution | 数据归属版权信息(显示在地图右下角)。 | String | HTML String | null |
| buffer | 切片缓冲区大小(像素)。用于解决切片边缘的符号裁剪问题。 | Number | 0 ~ 512 | 128 |
| tolerance | Douglas-Peucker 简化算法的容差。值越大细节越少,性能越好。 | Number | Number | 0.375 |
| cluster | 是否开启点聚合功能。 | Boolean | true, false | false |
| clusterRadius | 聚合半径(像素)。在此范围内的点会被合并。 | Number | Number | 50 |
| clusterMaxZoom | 聚合的最大缩放级别。超过此级别后,点将不再聚合。 | Number | 0 ~ 24 | maxzoom-1 |
| clusterMinPoints | 形成聚合所需的最小点数。少于此数量的点将不会聚合。 | Number | Number | 2 |
| clusterProperties | 定义聚合点的自定义属性计算规则(如 sum, max)。 | Object | Expression | - |
| lineMetrics | 是否计算线的距离度量(用于 line-gradient)。 | Boolean | true, false | false |
| generateId | 是否自动为每个 Feature 生成唯一的数字 ID。 | Boolean | true, false | false |
| promoteId | 指定 GeoJSON 属性中的某个字段作为 Feature 的唯一 ID。 | String | String | null |
示例场景:加载本地或远程的 GeoJSON 数据
html
<script>
map.on('load', () => {
// 添加数据源
map.addSource('mySource', {
type: 'geojson',
// 方式 A: 直接使用 URL
data: 'https://xxxx.geojson',
// 方式 B: 直接使用 GeoJSON 对象
// data: {
// "type": "FeatureCollection",
// "features": [{ ... }]
// },
})
// 添加图层引用该数据源
map.addLayer({
id: 'earthquakes-circle',
type: 'circle',
source: 'earthquakes',
paint: {
'circle-color': '#11b4da',
'circle-radius': 8,
'circle-stroke-width': 1,
'circle-stroke-color': '#fff',
},
})
})
</script>Raster Source
type: 'raster'
用于加载栅格切片(PNG/JPG 图片),如卫星图。
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|---|---|---|---|---|
| url | 指向 TileJSON 资源的 URL。 | String | URL String | - |
| tiles | 切片 URL 模板数组。如果未提供 url,则必须提供此参数。 | Array | Array | - |
| tileSize | 切片的像素尺寸。 | Number | Number | 512 |
| minzoom | 最小缩放级别。 | Number | 0 ~ 24 | 0 |
| maxzoom | 最大缩放级别。 | Number | 0 ~ 24 | 22 |
| scheme | 切片坐标系方案。 | String | 'xyz', 'tms' | 'xyz' |
| bounds | 限制切片请求的地理边界 [minLng, minLat, maxLng, maxLat]。 | Array | [Num, Num, Num, Num] | [-180,-90,180,90] |
| attribution | 数据版权信息。 | String | HTML String | null |
| volatile | 是否激进地卸载不可见切片。 | Boolean | true, false | false |
示例场景:加载瓦片地图。
html
<script>
map.on('load', () => {
map.addSource('myTilesSource', {
type: 'raster',
// XYZ 格式的瓦片服务
tiles: ['https://xxx/{z}/{y}/{x}.jpg'],
tileSize: 512,
})
map.addLayer({
id: 'myTilesLayer',
type: 'raster',
source: 'myTilesSource',
paint: {
'raster-opacity': 0.8,
},
})
})
</script>Image Source
type: 'image'
将单张图片叠加到地图的特定地理范围内(如建筑平面图、老地图)。
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|---|---|---|---|---|
| url | 图片的 URL。 | String | URL String | - |
| coordinates | 定义图片四个角的经纬度坐标(左上、右上、右下、左下)。 | Array | [[lng,lat], ...] | - |
示例场景:将一张静态图片贴在地图特定范围内。
html
<script>
map.on('load', () => {
map.addSource('myImageSource', {
type: 'image',
url: 'https://xxxx.gif',
coordinates: [
[-80.425, 46.437], // 左上
[-71.516, 46.437], // 右上
[-71.516, 37.936], // 右下
[-80.425, 37.936], // 左下
],
})
map.addLayer({
id: 'myImageLayer',
type: 'raster', // image source 使用 raster 图层渲染
source: 'myImageSource',
paint: {
'raster-fade-duration': 0,
},
})
})
</script>Video Source
type: 'video'
将视频文件叠加到地图上(如动态云图)。
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|---|---|---|---|---|
| urls | 视频文件的 URL 数组。支持多种格式以兼容不同浏览器。 | Array | Array | - |
| coordinates | 定义视频四个角的经纬度坐标(左上、右上、右下、左下)。 | Array | [[lng,lat], ...] | - |
示例场景:将视频贴在地图上。
html
<script>
map.on('load', () => {
map.addSource('myVideoSource', {
type: 'video',
urls: ['http:/xxxx/drone.mp4'],
coordinates: [
[-122.51596391201019, 37.56238816766053],
[-122.51467645168304, 37.56410183312965],
[-122.51309394836426, 37.563391708549425],
[-122.51423120498657, 37.56161849366671],
],
})
map.addLayer({
id: 'myVideoLayer',
type: 'raster',
source: 'myVideoSource',
})
})
</script>Canvas Source
type: 'canvas'
使用 HTML Canvas 元素作为数据源,适用于实时绘制的动态内容(如风场、波浪动画)。
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|---|---|---|---|---|
| canvas | canvas 的 DOM ID 或 HTMLCanvasElement 本身。 | ID, HTMLElement | - | - |
| coordinates | 定义 canvas 四个角的经纬度坐标(左上、右上、右下、左下)。 | Array | [[lng,lat], ...] | - |
| animate | 是否在每一帧都重新渲染 canvas(触发重绘)。 | Boolean | true, false | true |
示例场景:将canvas元素贴在地图上。
html
<canvas id="myCanvas" width="400" height="400" style="display: none;"></canvas>
<script>
map.on('load', () => {
// 1. 获取并绘制 Canvas
const canvas = document.getElementById('myCanvas')
const ctx = canvas.getContext('2d')
ctx.fillStyle = 'red'
ctx.fillRect(50, 50, 300, 300) // 画个红框
// 2. 添加 Source
map.addSource('canvas-source', {
type: 'canvas',
canvas: 'myCanvas',
animate: true,
coordinates: [
[-122.5159, 37.5623],
[-122.5146, 37.5641],
[-122.513, 37.5633],
[-122.5142, 37.5616],
],
})
// 3. 添加 Layer
map.addLayer({
id: 'canvas-layer',
type: 'raster',
source: 'canvas-source',
})
})
</script>