主题
数据源工厂
数据源工厂用于创建图层的底层数据提供者。
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| 参数 | 类型 | 描述 |
|---|---|---|
| dataOrUrl | string | object | GeoJSON 数据对象或 URL 字符串 |
| data | object | GeoJSON 数据(在 options 中) |
| cluster | boolean | 是否启用聚合 |
| clusterRadius | number | 聚合半径 |
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| 参数 | 类型 | 描述 |
|---|---|---|
| tiles | string[] | 矢量瓦片 URL 列表 |
| url | string | 矢量瓦片 URL 模板 |
ts
const src = G.vectorTileSource({
tiles: ['https://v.example.com/{z}/{x}/{y}.pbf'],
});G.rasterSource(options)
创建栅格瓦片数据源。
ts
G.rasterSource(options?: RasterSourceOptions): RasterTileSource| 参数 | 类型 | 描述 |
|---|---|---|
| url | string | 瓦片 URL 模板 |
| subdomains | string[] | 子域名列表 |
ts
const src = G.rasterSource({
url: 'https://tile.example.com/{z}/{x}/{y}.png',
});G.wms(options)
创建 WMS 数据源。
ts
G.wms(options: WmsOptions): WmsSource| 参数 | 类型 | 描述 |
|---|---|---|
| url | string | WMS 服务地址(必填) |
| layers | string[] | 图层名称列表 |
| version | string | WMS 版本 |
| format | string | 图片格式 |
| transparent | boolean | 是否透明 |
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| 参数 | 类型 | 描述 |
|---|---|---|
| url | string | WMTS 服务地址(必填) |
| layer | string | 图层名称 |
| tileMatrixSet | string | 瓦片矩阵集标识(默认 'default') |
ts
const src = G.wmts({
url: 'https://wmts.example.com',
layer: 'myLayer',
tileMatrixSet: 'default',
});G.terrainSource(options)
创建地形数据源。
ts
G.terrainSource(options?: TerrainSourceOptions): TerrainSource| 参数 | 类型 | 描述 |
|---|---|---|
| url | string | 地形瓦片 URL 模板 |
ts
const src = G.terrainSource({
url: 'https://terrain.example.com/{z}/{x}/{y}.terrain',
});