主题
控件工厂
控件工厂用于创建地图 UI 控件。所有控件都支持 getElement() 方法返回 DOM 元素。
G.zoomControl(options)
创建缩放控件(+/- 按钮)。
ts
G.zoomControl(options?: ZoomControlOptions): Control| 参数 | 类型 | 描述 |
|---|---|---|
| position | string | 位置('top-left'、'top-right'、'bottom-left'、'bottom-right') |
ts
const ctrl = G.zoomControl();
ctrl.getElement(); // HTMLElementG.scaleBarControl(options)
创建比例尺控件。
ts
G.scaleBarControl(options?: ScaleBarControlOptions): Control| 参数 | 类型 | 描述 |
|---|---|---|
| maxWidth | number | 最大宽度(像素) |
ts
const ctrl = G.scaleBarControl();G.overviewMapControl(options)
创建缩略图控件。
ts
G.overviewMapControl(options?: OverviewMapControlOptions): Controlts
const ctrl = G.overviewMapControl();G.mousePositionControl(options)
创建鼠标坐标显示控件。
ts
G.mousePositionControl(options?: MousePositionControlOptions): Control| 参数 | 类型 | 描述 |
|---|---|---|
| projection | string | 坐标系 |
| format | (coord) => string | 格式化函数 |
ts
const ctrl = G.mousePositionControl();G.legendControl(options)
创建图例控件。
ts
G.legendControl(options?: LegendControlOptions): Controlts
const ctrl = G.legendControl();G.searchControl(options)
创建搜索控件。
ts
G.searchControl(options?: SearchControlOptions): Control| 参数 | 类型 | 描述 |
|---|---|---|
| provider | string | 搜索服务提供方(如 'amap') |
| placeholder | string | 搜索框占位文字 |
ts
const ctrl = G.searchControl({ provider: 'amap' });G.contextMenuControl(options)
创建右键菜单控件。
ts
G.contextMenuControl(options?: ContextMenuControlOptions): Control| 参数 | 类型 | 描述 |
|---|---|---|
| items | object[] | 菜单项数组,每项包含 { label, action } |
ts
const ctrl = G.contextMenuControl({
items: [
{ label: '复制坐标', action: () => {} },
{ label: '定位', action: () => {} },
],
});G.scaleRangeSlider(options)
创建缩放范围滑块控件。
ts
G.scaleRangeSlider(options?: ScaleRangeSliderOptions): Controlts
const ctrl = G.scaleRangeSlider();G.compassControl(options)
创建指南针控件。
ts
G.compassControl(options?: CompassControlOptions): Controlts
const ctrl = G.compassControl();G.attributionControl(options)
创建版权归属控件。
ts
G.attributionControl(options?: AttributionControlOptions): Controlts
const ctrl = G.attributionControl();G.layerSwitcherControl(options)
创建图层切换控件。
ts
G.layerSwitcherControl(options?: LayerSwitcherControlOptions): Controlts
const ctrl = G.layerSwitcherControl();G.basemapSwitcherControl(options)
创建底图切换控件。
ts
G.basemapSwitcherControl(options?: BasemapSwitcherControlOptions): Controlts
const ctrl = G.basemapSwitcherControl();G.fullscreenControl(options)
创建全屏控件。
ts
G.fullscreenControl(options?: FullscreenControlOptions): Controlts
const ctrl = G.fullscreenControl();G.geolocationControl(options)
创建定位控件。
ts
G.geolocationControl(options?: GeolocationControlOptions): Control| 参数 | 类型 | 描述 |
|---|---|---|
| enableHighAccuracy | boolean | 是否高精度定位 |
| timeout | number | 超时时间(毫秒) |
ts
const ctrl = G.geolocationControl({ enableHighAccuracy: true });G.navigationControl(options)
创建导航控件。
ts
G.navigationControl(options?: NavigationControlOptions): Controlts
const ctrl = G.navigationControl();G.infoPanelControl(options)
创建信息面板控件。
ts
G.infoPanelControl(options?: InfoPanelControlOptions): Controlts
const ctrl = G.infoPanelControl();G.swipeControl(options)
创建卷帘对比控件。
ts
G.swipeControl(options?: SwipeControlOptions): Control| 参数 | 类型 | 描述 |
|---|---|---|
| direction | string | 滑动方向('horizontal'、'vertical') |
| position | number | 初始位置百分比 (0-100) |
ts
const ctrl = G.swipeControl({ direction: 'vertical', position: 50 });G.control(type, options)
通用控件工厂。
ts
G.control(type: string, options?: object): Control| 参数 | 类型 | 描述 |
|---|---|---|
| type | string | 控件类型 |
| options | object | 类型对应的选项 |
ts
G.control('zoom'); // 等效于 G.zoomControl()
G.control('compass'); // 等效于 G.compassControl()
G.control('scaleBar'); // 等效于 G.scaleBarControl()
G.control('attribution'); // 等效于 G.attributionControl()
G.control('layerSwitcher'); // 等效于 G.layerSwitcherControl()
G.control('basemapSwitcher');// 等效于 G.basemapSwitcherControl()
G.control('fullscreen'); // 等效于 G.fullscreenControl()
G.control('geolocation'); // 等效于 G.geolocationControl()
G.control('navigation'); // 等效于 G.navigationControl()
G.control('infoPanel'); // 等效于 G.infoPanelControl()
G.control('overviewMap'); // 等效于 G.overviewMapControl()
G.control('mousePosition'); // 等效于 G.mousePositionControl()
G.control('legend'); // 等效于 G.legendControl()
G.control('search'); // 等效于 G.searchControl()
G.control('contextMenu', { items: [...] }); // 等效于 G.contextMenuControl(...)未知类型默认回退为 zoom 控件。