主题
服务
服务模块提供地理编码、逆地理编码、搜索和路径规划等地图服务。所有服务方法返回 Promise。
G.services.geocode(address, options)
地理编码:将地址字符串转换为坐标。
ts
G.services.geocode(address: string, options?: object): Promise<GeocodeResult[]>| 参数 | 类型 | 描述 |
|---|---|---|
| address | string | 地址字符串(必填) |
| options | object | 附加选项 |
返回值:Promise<Array<{ lng, lat, address }>>
ts
const results = await G.services.geocode('北京');
// results: [{ lng: 116.397, lat: 39.908, address: '北京' }]G.services.regeocode(coord, options)
逆地理编码:将坐标转换为地址。
ts
G.services.regeocode(coord: Coordinate, options?: object): Promise<RegeocodeResult>| 参数 | 类型 | 描述 |
|---|---|---|
| coord | [number, number] | 坐标 [lng, lat](必填) |
| options | object | 附加选项 |
返回值:Promise<{ address: string }>
ts
const result = await G.services.regeocode([116.397, 39.908]);
// result: { address: '39.908, 116.397' }G.services.search(keyword, options)
地点搜索。
ts
G.services.search(keyword: string, options?: object): Promise<SearchResult[]>| 参数 | 类型 | 描述 |
|---|---|---|
| keyword | string | 搜索关键词(必填) |
| options | object | 附加选项(如范围、数量限制) |
返回值:Promise<Array<{ name, lng, lat }>>
ts
const results = await G.services.search('restaurant');
// results: [{ name: 'restaurant', lng: 116.397, lat: 39.908 }]G.services.routing(from, to, options)
路径规划。
ts
G.services.routing(from: Coordinate, to: Coordinate, options?: object): Promise<RoutingResult>| 参数 | 类型 | 描述 |
|---|---|---|
| from | [number, number] | 起点坐标 [lng, lat](必填) |
| to | [number, number] | 终点坐标 [lng, lat](必填) |
| options | object | 附加选项(如 mode: 'driving') |
返回值:Promise<{ distance, duration, steps }>
ts
const route = await G.services.routing([116.39, 39.90], [116.40, 39.91]);
// route: { distance: 1000, duration: 600, steps: [] }动画
动画模块提供要素动画效果。
G.animation.moveAlong(feature, path, options)
沿路径移动动画。
ts
G.animation.moveAlong(
feature: Feature,
path: Coordinate[],
options?: { duration?: number; loop?: boolean }
): AnimationHandle返回值:AnimationHandle { id: string, cancel(), finished: Promise<void> }
ts
const marker = G.marker({ coord: [0, 0] });
const handle = G.animation.moveAlong(marker, [[0, 0], [1, 1], [2, 2]], {
duration: 2000,
});
await handle.finished;G.animation.bounce(feature, options)
弹跳动画。
ts
G.animation.bounce(
feature: Feature,
options?: { height?: number; duration?: number; loop?: boolean }
): AnimationHandlets
const handle = G.animation.bounce(marker, { height: 10, duration: 500 });
await handle.finished;G.animation.breathe(feature, options)
呼吸缩放动画。
ts
G.animation.breathe(
feature: Feature,
options?: { scale?: number; duration?: number; loop?: boolean }
): AnimationHandlets
const handle = G.animation.breathe(marker, { scale: 1.5, duration: 1000 });
await handle.finished;AnimationHandle
所有动画返回统一的 AnimationHandle:
| 属性/方法 | 类型 | 说明 |
|---|---|---|
| id | string | 动画唯一标识 |
| cancel() | void | 取消动画 |
| finished | Promise<void> | 动画完成后 resolve |