主题
服务插件
@gmap/services 提供地图服务 API 封装。
安装
bash
npm install @gmap/services服务列表
地理编码 Geocoding
ts
import { Geocoding } from "@gmap/services";
// 地址 → 坐标
const result = await Geocoding.geocode("北京市天安门", {
provider: "amap",
key: "your-key",
});
// { lng: 116.397, lat: 39.908, address: '...' }
// 坐标 → 地址
const reverse = await Geocoding.regeocode([116.397, 39.908], {
provider: "amap",
key: "your-key",
});POI 搜索
ts
import { POISearch } from "@gmap/services";
const results = await POISearch.search("餐厅", {
provider: "amap",
key: "your-key",
center: [116.397, 39.908],
radius: 1000,
limit: 10,
});路径规划 Routing
ts
import { Routing } from "@gmap/services";
const route = await Routing.driving(
[116.397, 39.908], // 起点
[121.474, 31.23], // 终点
{ provider: "amap", key: "your-key" },
);
// { distance: 1200000, duration: 43200, steps: [...] }
// 其他模式
await Routing.walking(from, to, opts);
await Routing.transit(from, to, opts);
await Routing.riding(from, to, opts);天气 Weather
ts
import { Weather } from "@gmap/services";
const live = await Weather.live("北京", { provider: "amap", key: "your-key" });
// { temperature: 25, weather: '晴', humidity: 40, ... }
const forecast = await Weather.forecast("北京", {
provider: "amap",
key: "your-key",
days: 3,
});自动补全 Autocomplete
ts
import { Autocomplete } from "@gmap/services";
const suggestions = await Autocomplete.suggest("天安", {
provider: "amap",
key: "your-key",
center: [116.397, 39.908],
});行政区 District
ts
import { District } from "@gmap/services";
const result = await District.search("朝阳区", {
provider: "amap",
key: "your-key",
subdistrict: 2,
});交通 Traffic
ts
import { Traffic } from "@gmap/services";
const status = await Traffic.getStatus([116, 39, 117, 40], {
provider: "amap",
key: "your-key",
});IP 定位 IPPosition
ts
import { IPPosition } from "@gmap/services";
const location = await IPPosition.locate({ provider: "amap", key: "your-key" });
// { ip: '...', city: '北京', lng: 116.397, lat: 39.908 }支持的提供商
| 服务 | 本平台 | 高德 | 百度 | 天地图 |
|---|---|---|---|---|
| 地理编码 | 是 | 否 | 是 | 否 |
| POI | 是 | 否 | 是 | 否 |
| 路径规划 | 是 | 否 | 是 | 否 |
| 天气 | 是 | 否 | 否 | 否 |
| 自动补全 | 是 | 否 | 否 | 否 |
| 行政区 | 是 | 否 | 是 | 否 |
| 交通 | 是 | 否 | 否 | 否 |
| IP 定位 | 是 | 否 | 是 | 否 |