主题
图层类型 Layer
标准版提供 19+ 种图层类型,覆盖栅格瓦片、矢量瓦片、要素、图形、热力图、地形、3D 模型等所有场景。所有图层继承自核心 Layer 基类。
Layer(基类)
所有图层的公共基类,来自 @gmap/core。
属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
id | string | 自动生成 | 图层唯一标识 |
type | string | — | 图层类型(只读) |
source | Source | — | 关联的数据源 |
visible | boolean | true | 是否可见 |
opacity | number | 1 | 不透明度(0-1) |
zIndex | number | 0 | 图层顺序 |
基础选项
ts
interface BaseLayerOptions {
id?: string;
source?: Source;
visible?: boolean;
opacity?: number;
zIndex?: number;
minZoom?: number;
maxZoom?: number;
extent?: Extent;
}方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
setVisible(v) | v: boolean | this | 设置可见性 |
setOpacity(v) | v: number | this | 设置不透明度 |
setZIndex(v) | v: number | this | 设置层级 |
show() | — | this | 显示图层 |
hide() | — | this | 隐藏图层 |
toggle() | — | this | 切换可见性 |
addTo(map) | map: MapModel | this | 添加到地图 |
remove() | — | this | 从地图移除 |
toSpec() | — | object | 导出图层规格 |
TileLayer / RasterTileLayer
栅格瓦片图层,显示标准 XYZ/TMS 瓦片。
构造函数
ts
class TileLayer extends Layer {
readonly type = 'raster';
constructor(options?: BaseLayerOptions & {
url?: string;
subdomains?: string[];
tileSize?: number;
minZoom?: number;
maxZoom?: number;
});
}
// TileLayer 与 RasterTileLayer 是同一类型
const RasterTileLayer = TileLayer;属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
url | string | — | 瓦片 URL 模板 |
subdomains | string[] | [] | 子域名列表 |
tileSize | number | 256 | 瓦片尺寸(像素) |
示例
ts
import { TileLayer, RasterTileSource } from '@gmap/standard';
const source = new RasterTileSource({
url: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
minZoom: 0,
maxZoom: 18,
});
const layer = new TileLayer({
id: 'osm-base',
source,
opacity: 0.8,
});VectorTileLayer
矢量瓦片图层。
构造函数
ts
class VectorTileLayer extends Layer {
readonly type = 'vector-tile';
constructor(options?: BaseLayerOptions & {
sourceLayer?: string;
style?: unknown;
filter?: unknown;
});
}属性
| 属性 | 类型 | 说明 |
|---|---|---|
sourceLayer | string | undefined | 矢量瓦片中的图层名 |
style | unknown | 样式配置 |
filter | unknown | 过滤表达式 |
方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
setFilter(expr) | expr: any | this | 设置过滤表达式 |
示例
ts
import { VectorTileLayer, VectorTileSource } from '@gmap/standard';
const source = new VectorTileSource({
url: 'https://demotiles.maplibre.org/tiles/{z}/{x}/{y}.pbf',
});
const layer = new VectorTileLayer({
id: 'mvt-roads',
source,
sourceLayer: 'roads',
filter: ['==', 'class', 'primary'],
});FeatureLayer
要素图层,支持渲染器和属性查询。
构造函数
ts
class FeatureLayer extends Layer {
readonly type = 'feature';
constructor(options?: BaseLayerOptions & {
renderer?: Renderer;
definitionExpression?: string;
labeling?: unknown;
});
}属性
| 属性 | 类型 | 说明 |
|---|---|---|
renderer | Renderer | undefined | 渲染器 |
definitionExpression | string | undefined | 定义查询表达式(SQL-like) |
方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
setRenderer(renderer) | renderer: Renderer | this | 设置渲染器 |
setDefinitionExpression(expr) | expr: string | this | 设置定义查询 |
queryFeatures(options?) | options?: object | Promise<FeatureCollection> | 查询要素 |
示例
ts
import { FeatureLayer, GeoJsonSource, SimpleRenderer, FillSymbol } from '@gmap/standard';
const source = new GeoJsonSource({
id: 'districts',
data: districtsGeoJSON,
});
const layer = new FeatureLayer({
id: 'district-layer',
source,
renderer: new SimpleRenderer({
symbol: new FillSymbol({ color: '#3388ff', opacity: 0.5 }),
}),
definitionExpression: "status = 'active'",
});
const results = await layer.queryFeatures({ where: 'population > 100000' });GraphicsLayer
图形图层,用于动态添加/移除图形要素。
构造函数
ts
class GraphicsLayer extends Layer {
readonly type = 'graphics';
constructor(options?: BaseLayerOptions);
}方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
add(feature) | feature: unknown | this | 添加图形 |
addMany(features) | features: unknown[] | this | 批量添加 |
removeGraphic(featureOrId) | featureOrId: unknown | this | 移除图形 |
removeAll() | — | this | 移除所有图形 |
getById(id) | id: string | unknown | undefined | 按 ID 查找 |
forEach(fn) | fn: (feature: unknown) => void | void | 遍历所有图形 |
toGeoJSON() | — | object | 导出为 GeoJSON FeatureCollection |
示例
ts
import { GraphicsLayer, Point, MarkerSymbol } from '@gmap/standard';
const layer = new GraphicsLayer({ id: 'graphics' });
layer.add(new Point([116.397, 39.908]));
layer.addMany([
new Point([116.4, 39.91]),
new Point([116.42, 39.90]),
]);
console.log(layer.toGeoJSON());
layer.removeAll();ImageLayer
图片图层,在指定范围内显示图片。
构造函数
ts
class ImageLayer extends Layer {
readonly type = 'image';
constructor(options?: BaseLayerOptions & {
url?: string;
});
}示例
ts
import { ImageLayer } from '@gmap/standard';
const layer = new ImageLayer({
id: 'overlay-image',
url: '/images/aerial.jpg',
extent: [116.0, 39.0, 117.0, 40.0],
});HeatmapLayer
热力图图层。
构造函数
ts
class HeatmapLayer extends Layer {
readonly type = 'heatmap';
constructor(options?: BaseLayerOptions & {
radius?: number;
gradient?: string[];
intensity?: number;
weightField?: string;
});
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
radius | number | 25 | 热点半径(像素) |
gradient | string[] | — | 颜色渐变 |
intensity | number | 1 | 强度系数 |
weightField | string | undefined | — | 权重字段 |
示例
ts
import { HeatmapLayer, GeoJsonSource } from '@gmap/standard';
const source = new GeoJsonSource({ id: 'heat-data', data: pointsGeoJSON });
const layer = new HeatmapLayer({
id: 'heatmap',
source,
radius: 30,
gradient: ['#0000ff', '#00ffff', '#00ff00', '#ffff00', '#ff0000'],
intensity: 2,
weightField: 'count',
opacity: 0.8,
});TerrainLayer
地形图层。
构造函数
ts
class TerrainLayer extends Layer {
readonly type = 'terrain';
constructor(options?: BaseLayerOptions & {
exaggeration?: number;
});
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
exaggeration | number | 1 | 地形夸张系数 |
示例
ts
import { TerrainLayer, TerrainSource } from '@gmap/standard';
const source = new TerrainSource({
url: 'https://terrain.example.com/{z}/{x}/{y}.terrain',
type: 'quantized-mesh',
});
const layer = new TerrainLayer({
id: 'terrain',
source,
exaggeration: 1.5,
});Tileset3DLayer
3D Tiles 图层。
构造函数
ts
class Tileset3DLayer extends Layer {
readonly type = '3dtiles';
constructor(options?: BaseLayerOptions & {
maximumScreenSpaceError?: number;
});
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
maximumScreenSpaceError | number | 16 | 最大屏幕空间误差 |
示例
ts
import { Tileset3DLayer, Tileset3DSource } from '@gmap/standard';
const source = new Tileset3DSource({
url: 'https://3dtiles.example.com/tileset.json',
maximumScreenSpaceError: 8,
});
const layer = new Tileset3DLayer({
id: 'city-3d',
source,
maximumScreenSpaceError: 12,
});VideoLayer
视频图层,在地图上播放视频。
构造函数
ts
class VideoLayer extends Layer {
readonly type = 'video';
constructor(options?: BaseLayerOptions & {
url?: string;
loop?: boolean;
autoplay?: boolean;
});
}方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
play() | — | void | 播放 |
pause() | — | void | 暂停 |
stop() | — | void | 停止 |
seek(time) | time: number | void | 跳转到指定时间(秒) |
示例
ts
import { VideoLayer } from '@gmap/standard';
const layer = new VideoLayer({
id: 'weather-video',
url: '/videos/weather.mp4',
loop: true,
autoplay: true,
extent: [116, 39, 117, 40],
});
layer.play();
layer.seek(30);
layer.pause();CustomLayer
自定义图层,通过渲染函数完全控制绘制。
构造函数
ts
class CustomLayer extends Layer {
readonly type = 'custom';
constructor(options?: BaseLayerOptions & {
renderFunction?: (context: any) => void;
});
}方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
setRenderFunction(fn) | fn: (context: any) => void | this | 设置渲染函数 |
示例
ts
import { CustomLayer } from '@gmap/standard';
const layer = new CustomLayer({
id: 'custom-layer',
renderFunction: (ctx) => {
// 自定义绘制逻辑
console.log('rendering custom layer', ctx);
},
});
layer.setRenderFunction((ctx) => {
// 更新渲染逻辑
});PointCloudLayer
点云图层。
构造函数
ts
class PointCloudLayer extends Layer {
readonly type = 'pointcloud';
constructor(options?: BaseLayerOptions & {
pointSize?: number;
attenuation?: boolean;
});
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
pointSize | number | 1 | 点大小(像素) |
attenuation | boolean | false | 是否衰减 |
示例
ts
import { PointCloudLayer } from '@gmap/standard';
const layer = new PointCloudLayer({
id: 'pointcloud',
url: 'https://example.com/pointcloud.las',
pointSize: 2,
attenuation: true,
});VectorImageLayer
矢量图片图层。
构造函数
ts
class VectorImageLayer extends Layer {
readonly type = 'vector-image';
constructor(options?: BaseLayerOptions & {
url?: string;
opacity?: number;
});
}示例
ts
import { VectorImageLayer } from '@gmap/standard';
const layer = new VectorImageLayer({
id: 'vector-image',
url: 'https://example.com/vectortiles/{z}/{x}/{y}.pbf',
opacity: 0.7,
});ClusterLayer
聚合图层。
构造函数
ts
class ClusterLayer extends Layer {
readonly type = 'cluster';
constructor(options?: BaseLayerOptions & {
clusterRadius?: number;
maxClusterZoom?: number;
clusterSymbol?: Symbol;
});
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
clusterRadius | number | 50 | 聚合半径(像素) |
maxClusterZoom | number | — | 最大聚合缩放级别 |
示例
ts
import { ClusterLayer, GeoJsonSource, MarkerSymbol } from '@gmap/standard';
const source = new GeoJsonSource({ id: 'points', data: pointsGeoJSON });
const layer = new ClusterLayer({
id: 'clusters',
source,
clusterRadius: 80,
maxClusterZoom: 14,
clusterSymbol: new MarkerSymbol({ color: '#e74c3c', size: 20 }),
});DynamicLayer
动态图层,定期刷新数据。
构造函数
ts
class DynamicLayer extends Layer {
readonly type = 'dynamic';
constructor(options?: BaseLayerOptions & {
url?: string;
interval?: number;
});
}属性
| 属性 | 类型 | 说明 |
|---|---|---|
url | string | undefined | 数据 URL |
interval | number | undefined | 刷新间隔(毫秒) |
示例
ts
import { DynamicLayer } from '@gmap/standard';
const layer = new DynamicLayer({
id: 'traffic',
url: 'https://api.example.com/traffic/latest',
interval: 30000,
});GraticuleLayer
经纬网格图层。
构造函数
ts
class GraticuleLayer extends Layer {
readonly type = 'graticule';
constructor(options?: BaseLayerOptions & {
interval?: number;
strokeColor?: ColorLike;
});
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
interval | number | 10 | 网格间隔(度) |
strokeColor | ColorLike | '#ccc' | 网格线颜色 |
示例
ts
import { GraticuleLayer } from '@gmap/standard';
const layer = new GraticuleLayer({
id: 'graticule',
interval: 5,
strokeColor: 'rgba(0,0,0,0.2)',
});TileDebugLayer
瓦片调试图层,显示瓦片边界和坐标。
构造函数
ts
class TileDebugLayer extends Layer {
readonly type = 'tiledebug';
constructor(options?: BaseLayerOptions & {
tileSize?: number;
showCoords?: boolean;
});
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
tileSize | number | 256 | 瓦片尺寸 |
showCoords | boolean | false | 是否显示坐标 |
示例
ts
import { TileDebugLayer } from '@gmap/standard';
const layer = new TileDebugLayer({
id: 'debug-tiles',
tileSize: 512,
showCoords: true,
});TrafficLayer
交通图层。
构造函数
ts
class TrafficLayer extends Layer {
readonly type = 'traffic';
constructor(options?: BaseLayerOptions & {
provider?: 'amap' | 'bmap' | 'tianditu';
getTargetPosition?: () => Coordinate;
getWidth?: (level: number) => number;
});
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
provider | string | — | 数据提供方 |
示例
ts
import { TrafficLayer } from '@gmap/standard';
const layer = new TrafficLayer({
id: 'traffic',
provider: 'amap',
});BuildingLayer
建筑图层。
构造函数
ts
class BuildingLayer extends Layer {
readonly type = 'building';
constructor(options?: BaseLayerOptions & {
color?: ColorLike;
height?: number;
});
}属性
| 属性 | 类型 | 说明 |
|---|---|---|
color | ColorLike | undefined | 建筑颜色 |
height | number | undefined | 建筑高度 |
Deck.gl 图层
以下图层来自 Deck.gl 集成,用于高级可视化。
ArcLayer
ts
class ArcLayer extends Layer {
readonly type = 'arc';
constructor(options?: BaseLayerOptions & {
getSourcePosition?: () => Coordinate;
getTargetPosition?: () => Coordinate;
getSourceColor?: ColorLike;
getTargetColor?: ColorLike;
getWidth?: number;
elevationScale?: number;
});
}HexagonLayer
ts
class HexagonLayer extends Layer {
readonly type = 'hexagon';
constructor(options?: BaseLayerOptions & {
radius?: number;
elevationScale?: number;
elevationRange?: [number, number];
});
}GridLayer
ts
class GridLayer extends Layer {
readonly type = 'grid';
constructor(options?: BaseLayerOptions & {
cellSize?: number;
elevationScale?: number;
});
}TripsLayer
ts
class TripsLayer extends Layer {
readonly type = 'trips';
constructor(options?: BaseLayerOptions & {
getPath?: (d: unknown) => Coordinate[];
trailLength?: number;
currentTime?: number;
});
}GreatCircleLayer
ts
class GreatCircleLayer extends Layer {
readonly type = 'greatcircle';
constructor(options?: BaseLayerOptions & {
getSourcePosition?: () => Coordinate;
getTargetPosition?: () => Coordinate;
getSourceColor?: ColorLike;
getTargetColor?: ColorLike;
});
}H3HexagonLayer
ts
class H3HexagonLayer extends Layer {
readonly type = 'h3hexagon';
constructor(options?: BaseLayerOptions & {
hexagonColumn?: boolean;
elevationScale?: number;
});
}PanoramaLayer
ts
class PanoramaLayer extends Layer {
readonly type = 'panorama';
constructor(options?: BaseLayerOptions & {
provider?: string;
position?: Coordinate;
radius?: number;
});
}