Skip to content

控件工厂

控件工厂用于创建地图 UI 控件。所有控件都支持 getElement() 方法返回 DOM 元素。

G.zoomControl(options)

创建缩放控件(+/- 按钮)。

ts
G.zoomControl(options?: ZoomControlOptions): Control
参数类型描述
positionstring位置('top-left''top-right''bottom-left''bottom-right'
ts
const ctrl = G.zoomControl();
ctrl.getElement(); // HTMLElement

G.scaleBarControl(options)

创建比例尺控件。

ts
G.scaleBarControl(options?: ScaleBarControlOptions): Control
参数类型描述
maxWidthnumber最大宽度(像素)
ts
const ctrl = G.scaleBarControl();

G.overviewMapControl(options)

创建缩略图控件。

ts
G.overviewMapControl(options?: OverviewMapControlOptions): Control
ts
const ctrl = G.overviewMapControl();

G.mousePositionControl(options)

创建鼠标坐标显示控件。

ts
G.mousePositionControl(options?: MousePositionControlOptions): Control
参数类型描述
projectionstring坐标系
format(coord) => string格式化函数
ts
const ctrl = G.mousePositionControl();

G.legendControl(options)

创建图例控件。

ts
G.legendControl(options?: LegendControlOptions): Control
ts
const ctrl = G.legendControl();

G.searchControl(options)

创建搜索控件。

ts
G.searchControl(options?: SearchControlOptions): Control
参数类型描述
providerstring搜索服务提供方(如 'amap'
placeholderstring搜索框占位文字
ts
const ctrl = G.searchControl({ provider: 'amap' });

G.contextMenuControl(options)

创建右键菜单控件。

ts
G.contextMenuControl(options?: ContextMenuControlOptions): Control
参数类型描述
itemsobject[]菜单项数组,每项包含 { label, action }
ts
const ctrl = G.contextMenuControl({
  items: [
    { label: '复制坐标', action: () => {} },
    { label: '定位', action: () => {} },
  ],
});

G.scaleRangeSlider(options)

创建缩放范围滑块控件。

ts
G.scaleRangeSlider(options?: ScaleRangeSliderOptions): Control
ts
const ctrl = G.scaleRangeSlider();

G.compassControl(options)

创建指南针控件。

ts
G.compassControl(options?: CompassControlOptions): Control
ts
const ctrl = G.compassControl();

G.attributionControl(options)

创建版权归属控件。

ts
G.attributionControl(options?: AttributionControlOptions): Control
ts
const ctrl = G.attributionControl();

G.layerSwitcherControl(options)

创建图层切换控件。

ts
G.layerSwitcherControl(options?: LayerSwitcherControlOptions): Control
ts
const ctrl = G.layerSwitcherControl();

G.basemapSwitcherControl(options)

创建底图切换控件。

ts
G.basemapSwitcherControl(options?: BasemapSwitcherControlOptions): Control
ts
const ctrl = G.basemapSwitcherControl();

G.fullscreenControl(options)

创建全屏控件。

ts
G.fullscreenControl(options?: FullscreenControlOptions): Control
ts
const ctrl = G.fullscreenControl();

G.geolocationControl(options)

创建定位控件。

ts
G.geolocationControl(options?: GeolocationControlOptions): Control
参数类型描述
enableHighAccuracyboolean是否高精度定位
timeoutnumber超时时间(毫秒)
ts
const ctrl = G.geolocationControl({ enableHighAccuracy: true });

G.navigationControl(options)

创建导航控件。

ts
G.navigationControl(options?: NavigationControlOptions): Control
ts
const ctrl = G.navigationControl();

G.infoPanelControl(options)

创建信息面板控件。

ts
G.infoPanelControl(options?: InfoPanelControlOptions): Control
ts
const ctrl = G.infoPanelControl();

G.swipeControl(options)

创建卷帘对比控件。

ts
G.swipeControl(options?: SwipeControlOptions): Control
参数类型描述
directionstring滑动方向('horizontal''vertical'
positionnumber初始位置百分比 (0-100)
ts
const ctrl = G.swipeControl({ direction: 'vertical', position: 50 });

G.control(type, options)

通用控件工厂。

ts
G.control(type: string, options?: object): Control
参数类型描述
typestring控件类型
optionsobject类型对应的选项
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 控件。

四川省交通运输综合地理服务平台 地图开发框架