Skip to content

要素工厂

要素工厂用于创建可在地图上显示的地理要素。所有要素工厂返回的对象支持 .addTo(map) 链式调用。

G.marker(options)

创建点标记要素。

ts
G.marker(options: MarkerOptions): Feature
参数类型描述
coord[number, number]点坐标 [lng, lat](必填)
symbolobject标记符号(默认红色图钉)
popupstring弹窗内容
attributesRecord<string, any>自定义属性
draggableboolean是否可拖拽
ts
G.marker({
  coord: [116.397, 39.908],
  attributes: { name: '北京', population: 21500000 },
  popup: '<b>北京</b>',
}).addTo(map);

G.polyline(options)

创建折线要素。

ts
G.polyline(options: PolylineOptions): Feature
参数类型描述
pathCoordinate[]坐标数组 [[lng1, lat1], [lng2, lat2], ...](必填)
symbolobject线符号(默认蓝色 2px)
attributesRecord<string, any>自定义属性
ts
G.polyline({
  path: [[116, 39], [117, 40], [118, 41]],
  symbol: G.lineSymbol({ color: '#3498db', width: 3 }),
  attributes: { type: 'route' },
}).addTo(map);

G.polygon(options)

创建多边形要素。

ts
G.polygon(options: PolygonOptions): Feature
参数类型描述
ringsCoordinate[][] | Coordinate[]环形坐标数组。支持嵌套环 [外环] 或扁平环
symbolobject填充符号(默认半透明蓝色)
attributesRecord<string, any>自定义属性
ts
// 扁平环(自动包装为外环)
G.polygon({
  rings: [[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]],
  attributes: { area: 'zone1' },
}).addTo(map);

// 嵌套环(外环 + 内环挖洞)
G.polygon({
  rings: [
    [[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]],   // 外环
    [[2, 2], [8, 2], [8, 8], [2, 8], [2, 2]],       // 内环(挖洞)
  ],
}).addTo(map);

G.circle(options)

创建圆形要素。

ts
G.circle(options: CircleOptions): Feature
参数类型描述
center[number, number]圆心坐标 [lng, lat](必填)
radiusnumber半径(单位取决于坐标系,默认为米)(必填)
symbolobject填充符号(默认半透明蓝色)
attributesRecord<string, any>自定义属性
ts
G.circle({
  center: [116.397, 39.908],
  radius: 500,
  symbol: G.fillSymbol({ color: 'rgba(255,0,0,0.2)', outline: { color: '#f00' } }),
  attributes: { name: '服务范围' },
}).addTo(map);

G.rectangle(options)

创建矩形要素。

ts
G.rectangle(options: RectangleOptions): Feature
参数类型描述
extentExtent范围 [minX, minY, maxX, maxY](必填)
symbolobject填充符号(默认半透明蓝色)
attributesRecord<string, any>自定义属性
ts
G.rectangle({
  extent: [116.0, 39.0, 117.0, 40.0],
  symbol: G.fillSymbol({ color: 'rgba(0,128,0,0.2)' }),
  attributes: { id: 'rect1' },
}).addTo(map);

G.feature(options)

创建通用要素。

ts
G.feature(options: FeatureOptions): Feature
参数类型描述
geometryobject几何对象(GeoJSON 格式或 G.geom.* 创建的对象)
symbolobject符号
attributesRecord<string, any>自定义属性
ts
G.feature({
  geometry: G.geom.point([116, 39]),
  symbol: G.markerSymbol({ color: '#e74c3c', size: 12 }),
  attributes: { tag: 'test' },
}).addTo(map);

G.featureCollection(features)

创建要素集合。

ts
G.featureCollection(features?: Feature[]): FeatureCollection
参数类型描述
featuresFeature[]要素数组(默认为空数组)
ts
const fc = G.featureCollection([
  G.marker({ coord: [0, 0] }),
  G.marker({ coord: [1, 1] }),
]);

链式调用

所有要素创建后可通过 .addTo(map) 添加到地图:

ts
G.marker({ coord: [116, 39] }).addTo(map);
G.polyline({ path: [[116, 39], [117, 40]] }).addTo(map);
G.polygon({ rings: [[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]] }).addTo(map);

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