主题
符号类型 Symbol
标准版提供 12 种符号类型,用于定义要素的视觉表现。所有类型继承自抽象基类 Symbol。
Symbol(抽象基类)
所有符号的公共基类。
ts
abstract class Symbol {
readonly type: string;
visible: boolean;
shadow?: { color: ColorLike; blur?: number; offsetX?: number; offsetY?: number };
glow?: { color: ColorLike; radius?: number; intensity?: number };
abstract clone(): Symbol;
toJSON(): Record<string, unknown>;
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
type | string | — | 符号类型标识(只读) |
visible | boolean | true | 是否可见 |
shadow | object | undefined | — | 阴影配置 |
shadow.color | ColorLike | — | 阴影颜色 |
shadow.blur | number | — | 模糊半径 |
shadow.offsetX | number | — | X 偏移 |
shadow.offsetY | number | — | Y 偏移 |
glow | object | undefined | — | 发光效果配置 |
glow.color | ColorLike | — | 发光颜色 |
glow.radius | number | — | 发光半径 |
glow.intensity | number | — | 发光强度 |
方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
clone() | — | Symbol | 深拷贝(抽象方法) |
toJSON() | — | Record<string, unknown> | 序列化为 JSON |
FillSymbol
填充符号,用于面状要素。
构造函数
ts
class FillSymbol extends Symbol {
readonly type = 'fill';
constructor(options?: FillSymbolOptions);
}ts
interface FillSymbolOptions {
color?: ColorLike;
opacity?: number;
outline?: {
color?: ColorLike;
width?: number;
dashArray?: number[];
cap?: 'butt' | 'round' | 'square';
join?: 'miter' | 'round' | 'bevel';
};
pattern?: {
type: 'image' | 'hatch';
image?: string;
hatch?: 'cross' | 'diagonal' | 'vertical' | 'horizontal';
scale?: number;
};
fillType?: 'solid' | 'hatch' | 'pattern';
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
color | ColorLike | '#3388ff' | 填充颜色 |
opacity | number | 1 | 不透明度(0-1) |
outline | object | undefined | — | 边线配置 |
fillType | string | 'solid' | 填充类型 |
示例
ts
import { FillSymbol } from '@gmap/standard';
const fill = new FillSymbol({
color: '#ff6600',
opacity: 0.7,
outline: { color: '#333', width: 2 },
fillType: 'solid',
});LineSymbol
线符号,用于线状要素。
构造函数
ts
class LineSymbol extends Symbol {
readonly type = 'line';
constructor(options?: LineSymbolOptions);
}ts
interface LineSymbolOptions {
color?: ColorLike;
width?: number;
opacity?: number;
cap?: 'butt' | 'round' | 'square';
join?: 'miter' | 'round' | 'bevel';
miterLimit?: number;
dashArray?: number[];
dashOffset?: number;
arrow?: boolean;
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
color | ColorLike | '#3388ff' | 线颜色 |
width | number | 2 | 线宽(像素) |
opacity | number | 1 | 不透明度 |
cap | string | 'butt' | 端点样式 |
join | string | 'miter' | 连接样式 |
miterLimit | number | 10 | 尖角限制 |
dashArray | number[] | [] | 虚线模式 |
dashOffset | number | 0 | 虚线偏移 |
arrow | boolean | false | 是否显示箭头 |
方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
setDashArray(pattern) | pattern: number[] | this | 设置虚线模式 |
示例
ts
import { LineSymbol } from '@gmap/standard';
const line = new LineSymbol({
color: '#e74c3c',
width: 3,
cap: 'round',
join: 'round',
dashArray: [10, 5],
arrow: true,
});
line.setDashArray([20, 10, 5, 10]);MarkerSymbol
标记符号,用于点状要素。
构造函数
ts
class MarkerSymbol extends Symbol {
readonly type = 'marker';
constructor(options?: MarkerSymbolOptions);
}ts
interface MarkerSymbolOptions {
color?: ColorLike;
size?: number;
opacity?: number;
shape?: 'circle' | 'square' | 'triangle' | 'diamond' | 'cross' | 'star';
rotation?: number;
anchor?: 'center' | 'left' | 'right' | 'top' | 'bottom' |
'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
outline?: { color?: ColorLike; width?: number };
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
color | ColorLike | '#3388ff' | 标记颜色 |
size | number | 8 | 标记大小(像素) |
opacity | number | 1 | 不透明度 |
shape | string | 'circle' | 形状 |
rotation | number | 0 | 旋转角度(度) |
anchor | string | 'center' | 锚点位置 |
outline | object | undefined | — | 边线配置 |
方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
setColor(color) | color: ColorLike | this | 设置颜色 |
clone() | — | MarkerSymbol | 深拷贝 |
toJSON() | — | Record<string, unknown> | 序列化 |
示例
ts
import { MarkerSymbol } from '@gmap/standard';
const marker = new MarkerSymbol({
color: '#e74c3c',
size: 12,
shape: 'star',
rotation: 45,
anchor: 'center',
outline: { color: '#fff', width: 2 },
});
marker.setColor('#2ecc71');TextSymbol
文本符号,用于标注。
构造函数
ts
class TextSymbol extends Symbol {
readonly type = 'text';
constructor(options?: TextSymbolOptions);
}ts
interface TextSymbolOptions {
text?: string;
fontSize?: number;
fontFamily?: string;
fontWeight?: string;
color?: ColorLike;
haloColor?: ColorLike;
haloWidth?: number;
backgroundColor?: ColorLike;
padding?: [number, number, number?, number?];
placement?: 'point' | 'line';
anchor?: string;
offsetX?: number;
offsetY?: number;
overflow?: boolean;
maxWidth?: number;
lineHeight?: number;
letterSpacing?: number;
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
text | string | '' | 文本内容 |
fontSize | number | 14 | 字号 |
fontFamily | string | 'sans-serif' | 字体 |
fontWeight | string | 'normal' | 字重 |
color | ColorLike | '#333333' | 文本颜色 |
haloColor | ColorLike | undefined | — | 光晕颜色 |
haloWidth | number | 0 | 光晕宽度 |
backgroundColor | ColorLike | undefined | — | 背景色 |
padding | [number, number, number?, number?] | [4, 4] | 内边距 |
placement | string | 'point' | 放置模式 |
anchor | string | 'center' | 锚点 |
offsetX | number | 0 | X 偏移 |
offsetY | number | 0 | Y 偏移 |
overflow | boolean | false | 是否溢出 |
maxWidth | number | 0 | 最大宽度(0=不限) |
lineHeight | number | 1.2 | 行高 |
letterSpacing | number | 0 | 字间距 |
方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
setText(text) | text: string | this | 设置文本 |
setColor(color) | color: ColorLike | this | 设置颜色 |
clone() | — | TextSymbol | 深拷贝 |
toJSON() | — | Record<string, unknown> | 序列化 |
示例
ts
import { TextSymbol } from '@gmap/standard';
const text = new TextSymbol({
text: '北京市',
fontSize: 16,
fontFamily: 'Arial',
fontWeight: 'bold',
color: '#333',
haloColor: '#fff',
haloWidth: 2,
backgroundColor: 'rgba(255,255,255,0.8)',
padding: [6, 8],
placement: 'point',
offsetX: 10,
offsetY: -5,
});
text.setText('上海市');PictureSymbol
图片符号,用于以图片渲染点要素。
构造函数
ts
class PictureSymbol extends Symbol {
readonly type = 'picture';
constructor(options: {
url: string;
width?: number;
height?: number;
opacity?: number;
rotation?: number;
});
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
url | string | — | 图片 URL |
width | number | 32 | 宽度(像素) |
height | number | 32 | 高度(像素) |
opacity | number | 1 | 不透明度 |
rotation | number | 0 | 旋转角度 |
示例
ts
import { PictureSymbol } from '@gmap/standard';
const pic = new PictureSymbol({
url: '/icons/marker.png',
width: 24,
height: 24,
opacity: 0.9,
rotation: 30,
});IconSymbol
图标符号,用于地图图标标记。
构造函数
ts
class IconSymbol extends Symbol {
readonly type = 'icon';
constructor(options: {
url: string;
size?: [number, number];
anchor?: [number, number];
offset?: [number, number];
});
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
url | string | — | 图标 URL |
size | [number, number] | [32, 32] | 图标尺寸 |
anchor | [number, number] | [16, 16] | 锚点(像素) |
offset | [number, number] | [0, 0] | 偏移(像素) |
示例
ts
import { IconSymbol } from '@gmap/standard';
const icon = new IconSymbol({
url: '/icons/pin.png',
size: [48, 48],
anchor: [24, 48],
offset: [0, -24],
});GradientFillSymbol
渐变填充符号,继承自 FillSymbol。
构造函数
ts
class GradientFillSymbol extends FillSymbol {
readonly type = 'gradient-fill';
constructor(options: {
gradientType: 'linear' | 'radial';
stops: GradientStop[];
angle?: number;
});
}ts
interface GradientStop {
offset: number; // 0-1
color: ColorLike;
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
gradientType | 'linear' | 'radial' | — | 渐变类型 |
stops | GradientStop[] | — | 渐变停止点 |
angle | number | 0 | 渐变角度(度) |
fillType | string | gradientType | 填充类型 |
示例
ts
import { GradientFillSymbol } from '@gmap/standard';
const gradient = new GradientFillSymbol({
gradientType: 'linear',
stops: [
{ offset: 0, color: '#ff0000' },
{ offset: 0.5, color: '#ffff00' },
{ offset: 1, color: '#00ff00' },
],
angle: 90,
});CompositeSymbol
复合符号,可组合多个符号叠加渲染。
构造函数
ts
class CompositeSymbol extends Symbol {
readonly type = 'composite';
constructor(options?: { symbols?: Symbol[] });
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
symbols | Symbol[] | [] | 子符号数组 |
方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
addSymbol(symbol) | symbol: Symbol | this | 添加子符号 |
clone() | — | CompositeSymbol | 深拷贝 |
示例
ts
import { CompositeSymbol, FillSymbol, LineSymbol } from '@gmap/standard';
const composite = new CompositeSymbol({
symbols: [
new FillSymbol({ color: 'rgba(51,136,255,0.4)' }),
new LineSymbol({ color: '#3388ff', width: 2 }),
],
});
composite.addSymbol(new LineSymbol({ color: '#fff', width: 1, dashArray: [5, 5] }));ModelSymbol
3D 模型符号,用于在地图上加载和显示 3D 模型。
构造函数
ts
class ModelSymbol extends Symbol {
readonly type = 'model';
constructor(options: {
url: string;
scale?: [number, number, number];
rotation?: [number, number, number];
color?: ColorLike;
opacity?: number;
animation?: string;
animationSpeed?: number;
castShadow?: boolean;
receiveShadow?: boolean;
});
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
url | string | — | 模型 URL(glTF/glb) |
scale | [number, number, number] | [1, 1, 1] | 缩放 [x, y, z] |
rotation | [number, number, number] | [0, 0, 0] | 旋转 [rx, ry, rz](度) |
color | ColorLike | undefined | — | 模型颜色 |
opacity | number | 1 | 不透明度 |
animation | string | undefined | — | 动画名称 |
animationSpeed | number | 1 | 动画速度 |
castShadow | boolean | false | 是否投射阴影 |
receiveShadow | boolean | false | 是否接收阴影 |
示例
ts
import { ModelSymbol } from '@gmap/standard';
const model = new ModelSymbol({
url: '/models/building.glb',
scale: [1, 1, 1],
rotation: [0, 45, 0],
color: '#6699cc',
opacity: 0.9,
animation: 'idle',
animationSpeed: 1.5,
castShadow: true,
receiveShadow: true,
});BillboardSymbol
广告牌符号,始终面向相机的 3D 标注。
构造函数
ts
class BillboardSymbol extends Symbol {
readonly type = 'billboard';
constructor(options: {
image?: string | HTMLCanvasElement;
text?: string;
fontSize?: number;
color?: ColorLike;
backgroundColor?: ColorLike;
padding?: number;
scale?: number;
pixelOffset?: [number, number];
eyeOffset?: [number, number, number];
rotation?: number;
scaleByDistance?: boolean;
alwaysOnTop?: boolean;
});
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
image | string | HTMLCanvasElement | undefined | — | 图片 |
text | string | '' | 文本 |
fontSize | number | 16 | 字号 |
color | ColorLike | '#ffffff' | 文本颜色 |
backgroundColor | ColorLike | 'rgba(0,0,0,0.5)' | 背景色 |
padding | number | 8 | 内边距(像素) |
scale | number | 1 | 缩放 |
pixelOffset | [number, number] | [0, 0] | 像素偏移 |
eyeOffset | [number, number, number] | [0, 0, 0] | 视点偏移 |
rotation | number | 0 | 旋转角度 |
scaleByDistance | boolean | false | 是否随距离缩放 |
alwaysOnTop | boolean | false | 是否始终在最上层 |
示例
ts
import { BillboardSymbol } from '@gmap/standard';
const billboard = new BillboardSymbol({
text: '天安门广场',
fontSize: 18,
color: '#fff',
backgroundColor: 'rgba(220,50,50,0.8)',
padding: 10,
scale: 1.5,
pixelOffset: [0, -30],
alwaysOnTop: true,
});DivIconSymbol
HTML Div 图标符号,使用自定义 HTML 渲染标记。
构造函数
ts
class DivIconSymbol extends Symbol {
readonly type = 'div-icon';
constructor(options: {
html: string;
width?: number;
height?: number;
anchor?: [number, number];
className?: string;
});
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
html | string | — | HTML 内容 |
width | number | 30 | 宽度(像素) |
height | number | 30 | 高度(像素) |
anchor | [number, number] | [15, 15] | 锚点(像素) |
className | string | '' | CSS 类名 |
示例
ts
import { DivIconSymbol } from '@gmap/standard';
const divIcon = new DivIconSymbol({
html: '<div class="my-marker"><span>12</span></div>',
width: 40,
height: 40,
anchor: [20, 20],
className: 'custom-marker',
});CSS
css
.my-marker {
width: 40px;
height: 40px;
border-radius: 50%;
background: #e74c3c;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
box-shadow: 0 2px 6px rgba(0,0,0,0.3);
}