Skip to content

数据源工厂

数据源工厂用于创建图层的底层数据提供者。

G.source(type, options)

通用数据源工厂,根据类型创建对应数据源。

ts
G.source(type: string, options?: object): Source

支持的类型:

type说明返回类型
'raster'栅格瓦片数据源RasterTileSource
'vector'矢量瓦片数据源VectorTileSource
'geojson'GeoJSON 数据源GeoJsonSource
'wms'WMS 服务数据源WmsSource
'wmts'WMTS 服务数据源WmtsSource
'terrain'地形数据源TerrainSource
ts
const src = G.source('raster', { url: 'https://tile.example.com/{z}/{x}/{y}.png' });
const src2 = G.source('geojson', { data: { type: 'FeatureCollection', features: [] } });

G.geojson(dataOrUrl, options)

创建 GeoJSON 数据源。

ts
G.geojson(dataOrUrl: string | object, options?: object): GeoJsonSource
参数类型描述
dataOrUrlstring | objectGeoJSON 数据对象或 URL 字符串
dataobjectGeoJSON 数据(在 options 中)
clusterboolean是否启用聚合
clusterRadiusnumber聚合半径
ts
// 从对象创建
const src = G.geojson({ type: 'FeatureCollection', features: [] });

// 从 URL 创建
const src2 = G.geojson('https://example.com/data.geojson');

// 带聚合
const src3 = G.geojson(myData, { cluster: true, clusterRadius: 50 });

G.vectorTileSource(options)

创建矢量瓦片数据源。

ts
G.vectorTileSource(options: VectorTileSourceOptions): VectorTileSource
参数类型描述
tilesstring[]矢量瓦片 URL 列表
urlstring矢量瓦片 URL 模板
ts
const src = G.vectorTileSource({
  tiles: ['https://v.example.com/{z}/{x}/{y}.pbf'],
});

G.rasterSource(options)

创建栅格瓦片数据源。

ts
G.rasterSource(options?: RasterSourceOptions): RasterTileSource
参数类型描述
urlstring瓦片 URL 模板
subdomainsstring[]子域名列表
ts
const src = G.rasterSource({
  url: 'https://tile.example.com/{z}/{x}/{y}.png',
});

G.wms(options)

创建 WMS 数据源。

ts
G.wms(options: WmsOptions): WmsSource
参数类型描述
urlstringWMS 服务地址(必填)
layersstring[]图层名称列表
versionstringWMS 版本
formatstring图片格式
transparentboolean是否透明
ts
const src = G.wms({
  url: 'https://wms.example.com',
  layers: ['layer1', 'layer2'],
  transparent: true,
});

G.wmts(options)

创建 WMTS 数据源。

ts
G.wmts(options: WmtsOptions): WmtsSource
参数类型描述
urlstringWMTS 服务地址(必填)
layerstring图层名称
tileMatrixSetstring瓦片矩阵集标识(默认 'default'
ts
const src = G.wmts({
  url: 'https://wmts.example.com',
  layer: 'myLayer',
  tileMatrixSet: 'default',
});

G.terrainSource(options)

创建地形数据源。

ts
G.terrainSource(options?: TerrainSourceOptions): TerrainSource
参数类型描述
urlstring地形瓦片 URL 模板
ts
const src = G.terrainSource({
  url: 'https://terrain.example.com/{z}/{x}/{y}.terrain',
});

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