@tianditu/services 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Demo Macro
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # @tianditu/services
2
+
3
+ ![npm version](https://img.shields.io/npm/v/@tianditu/services)
4
+ ![npm downloads](https://img.shields.io/npm/dw/@tianditu/services)
5
+ ![npm license](https://img.shields.io/npm/l/@tianditu/services)
6
+
7
+ > 天地图 REST 服务的类型化客户端,Node 与浏览器均可运行。
8
+ > 覆盖地名搜索、驾车/公交规划、地理编码、逆地理编码、行政区划与静态地图:每个接口都有类型化的请求与响应,状态码语义与官方一致,与地图运行时解耦、可独立使用。
9
+
10
+ ## 特性
11
+
12
+ - 🔌 **独立可用** — 纯请求封装,与地图运行时解耦;Node 与浏览器均可运行
13
+ - 💪 **完整类型** — 每个接口都有类型化请求与响应(`SearchResult`、`DriveResult`、`TransitResult`、`GeoCodingResult`、`AdministrativeResult` 等)
14
+ - 🧭 **接口齐全** — 地名搜索、驾车/公交规划、地理编码、逆地理编码、行政区划与静态地图
15
+ - 🛟 **状态语义** — 结果状态码(`infocode`)原样暴露,含义与官方一致
16
+
17
+ ## 安装
18
+
19
+ ```bash
20
+ # npm
21
+ $ npm install @tianditu/services
22
+
23
+ # yarn
24
+ $ yarn add @tianditu/services
25
+
26
+ # pnpm
27
+ $ pnpm add @tianditu/services
28
+ ```
29
+
30
+ ## 快速开始
31
+
32
+ ```typescript
33
+ import { defineTianditu } from "@tianditu/services";
34
+
35
+ const tianditu = defineTianditu({
36
+ tk: "你的服务器端密钥",
37
+ });
38
+
39
+ const result = await tianditu.search({
40
+ keyWord: "北京站",
41
+ // 地名搜索V2.0:普通搜索(含地铁公交)
42
+ queryType: 1,
43
+ start: 0,
44
+ count: 10,
45
+ });
46
+
47
+ if (result.status.infocode === 1000) {
48
+ for (const poi of result.pois ?? []) {
49
+ console.log(poi.name, poi.address, poi.lonlat);
50
+ }
51
+ }
52
+ ```
53
+
54
+ ## API
55
+
56
+ | 方法 | 参数 | 说明 |
57
+ | --------------------------- | ------------------------- | ------------- |
58
+ | `search(postStr)` | `SearchPostStr` | 地名搜索 V2.0 |
59
+ | `drive(postStr)` | `DrivePostStr` | 驾车路线规划 |
60
+ | `transit(postStr)` | `TransitPostStr` | 公交路线规划 |
61
+ | `geoCoding(ds)` | `GeoCodingDs` | 地理编码 |
62
+ | `reverseGeoCoding(postStr)` | `ReverseGeoCodingPostStr` | 逆地理编码 |
63
+ | `administrative(params)` | `AdministrativeParams` | 行政区划查询 |
64
+ | `staticImage(params)` | `StaticImageParams` | 静态地图 |
65
+
66
+ ## 相关包
67
+
68
+ - [@tianditu/core](https://www.npmjs.com/package/@tianditu/core) — 框架无关内核
69
+ - [@tianditu/vue](https://www.npmjs.com/package/@tianditu/vue) — Vue 3 组件适配层
70
+ - [@tianditu/web-components](https://www.npmjs.com/package/@tianditu/web-components) — Web Components 适配层
71
+
72
+ ## 许可
73
+
74
+ - [MIT](LICENSE) © [Demo Macro](https://www.demomacro.com/)
@@ -0,0 +1,569 @@
1
+ import { FetchOptions, FetchRequest } from "ofetch";
2
+ //#region src/types/administrative.d.ts
3
+ /**
4
+ * 行政区划 V2.0(官方文档 server/administrative2.html)。
5
+ * 端点 /v2/administrative,query 参数风格。
6
+ */
7
+ interface AdministrativeParams {
8
+ /**
9
+ * 行政区划名称或编码;仅名称支持模糊查询。
10
+ * 仅一个字符时只返回 suggestion 字段,不返回 district 字段。
11
+ */
12
+ keyword: string;
13
+ /** 显示下级行政区级数:0 不返回、1 下一级、2 下两级、3 下三级 */
14
+ childLevel?: 0 | 1 | 2 | 3;
15
+ /** 是否需要轮廓数据 */
16
+ extensions?: boolean;
17
+ }
18
+ interface AdministrativeResult {
19
+ /** 返回描述 */
20
+ message: string;
21
+ /** 0 正常(官方文档同页亦标注 100/101 码表,以实际响应为准) */
22
+ status: number;
23
+ /** 行政区划信息 */
24
+ data: AdministrativeData;
25
+ }
26
+ interface AdministrativeData {
27
+ /** 建议搜索词(模糊匹配多条时返回;仅一条时为空) */
28
+ suggestion: string[];
29
+ /** 行政区划信息 */
30
+ district: AdministrativeDistrict[];
31
+ }
32
+ interface AdministrativeDistrict {
33
+ /** 行政区划名称 */
34
+ name: string;
35
+ /** 行政区划编码 */
36
+ gb: string;
37
+ /** 轮廓数据(extensions=true 时返回) */
38
+ boundary?: string;
39
+ /** 中心点坐标 */
40
+ center: AdministrativeCenter;
41
+ /** 行政区划级别:5 国家级、4 省级、3 市级、2 区县级 */
42
+ level: 2 | 3 | 4 | 5;
43
+ /** 下级行政区划信息 */
44
+ children: AdministrativeChild[];
45
+ }
46
+ interface AdministrativeChild {
47
+ /** 行政区划名称 */
48
+ name: string;
49
+ /** 行政区划编码 */
50
+ gb: string;
51
+ /** 中心点坐标 */
52
+ center: AdministrativeCenter;
53
+ /** 行政区划级别:5 国家级、4 省级、3 市级、2 区县级 */
54
+ level: 2 | 3 | 4 | 5;
55
+ /** 下级行政区划信息 */
56
+ children: AdministrativeChild[];
57
+ }
58
+ interface AdministrativeCenter {
59
+ /** 经度 */
60
+ lng: number;
61
+ /** 纬度 */
62
+ lat: number;
63
+ }
64
+ //#endregion
65
+ //#region src/types/drive.d.ts
66
+ /**
67
+ * 驾车规划(官方文档 server/drive.html)。
68
+ * 端点 /drive?type=search,postStr 为 JSON 串。
69
+ */
70
+ interface DrivePostStr {
71
+ /** 起点经纬度 "lng,lat" */
72
+ orig: string;
73
+ /** 终点经纬度 "lng,lat" */
74
+ dest: string;
75
+ /** 途经点 "lng,lat;lng,lat" */
76
+ mid?: string;
77
+ /** 导航路线类型:0 最快、1 最短、2 避开高速、3 步行(默认 0) */
78
+ style?: "0" | "1" | "2" | "3";
79
+ }
80
+ /**
81
+ * 驾车规划响应。官方文档页的响应示例容器已失效(codeDemo 404),
82
+ * 无响应结构文档,故不臆造类型;待真实环境取样后补充。
83
+ */
84
+ type DriveResult = Record<string, unknown>;
85
+ //#endregion
86
+ //#region src/types/geocoder.d.ts
87
+ /**
88
+ * 地理编码(官方文档 server/geocodinginterface.html 正向、server/geocoding.html 逆向)。
89
+ * 端点均为 /geocoder:正向 ds 参数、逆向 postStr 参数(type=geocode)。
90
+ */
91
+ /** 正向地理编码请求 */
92
+ interface GeoCodingDs {
93
+ /** 请求关键字(结构化地址,仅限国内) */
94
+ keyWord: string;
95
+ }
96
+ interface GeoCodingResult {
97
+ /** 返回状态:0 正常、101 结果为空、404 出错 */
98
+ status: string;
99
+ /** 返回信息:OK 正常,其他异常 */
100
+ msg: string;
101
+ /** 地址信息 */
102
+ location: GeoCodingLocation;
103
+ }
104
+ interface GeoCodingLocation {
105
+ /** 坐标点显示经度 */
106
+ lon: number;
107
+ /** 坐标点显示纬度 */
108
+ lat: number;
109
+ /** 类别名称 */
110
+ level?: string;
111
+ /** 附近相似点(开启周边查询时返回) */
112
+ typeRound?: unknown[];
113
+ }
114
+ /** 逆地理编码请求 */
115
+ interface ReverseGeoCodingPostStr {
116
+ /** 坐标的 x 值 */
117
+ lon: number;
118
+ /** 坐标的 y 值 */
119
+ lat: number;
120
+ /** 接口版本,默认 1 */
121
+ ver?: number;
122
+ }
123
+ interface ReverseGeoCodingResult {
124
+ /** 状态:0 正确、1 错误、404 出错 */
125
+ status: string;
126
+ /** 响应信息(OK 为有信息) */
127
+ msg: string;
128
+ /** 响应的具体信息(有结果时返回) */
129
+ result?: ReverseGeoCodingDetail;
130
+ }
131
+ interface ReverseGeoCodingDetail {
132
+ /** 此点的具体信息(分类) */
133
+ addressComponent: ReverseGeoCodingAddressComponent;
134
+ /** 详细地址 */
135
+ formatted_address: string;
136
+ /** 此点坐标 */
137
+ location: ReverseGeoCodingPoint;
138
+ }
139
+ interface ReverseGeoCodingAddressComponent {
140
+ /** 此点最近地点信息 */
141
+ address: string;
142
+ /** 距最近地点信息的距离 */
143
+ address_distince: number;
144
+ /** 此点在最近地点信息的方向 */
145
+ address_position: string;
146
+ /** 此点所在国家或城市或区县 */
147
+ city: string;
148
+ /** 距离此点最近的 poi 点 */
149
+ poi: string;
150
+ /** 距最近 poi 点的距离 */
151
+ poi_distince: number;
152
+ /** 此点在最近 poi 点的方向 */
153
+ poi_position: string;
154
+ /** 距离此点最近的路 */
155
+ road: string;
156
+ /** 此点距此路的距离 */
157
+ road_distince: number;
158
+ }
159
+ interface ReverseGeoCodingPoint {
160
+ /** 此点坐标 x 值 */
161
+ lon: string;
162
+ /** 此点坐标 y 值 */
163
+ lat: string;
164
+ }
165
+ //#endregion
166
+ //#region src/types/search.d.ts
167
+ /**
168
+ * 地名搜索 V2.0(官方文档 server/search2.html)。
169
+ * 端点 /v2/search?type=query,postStr 为 JSON 串。
170
+ */
171
+ /**
172
+ * 搜索请求参数。必填项随 queryType 不同:
173
+ * - 1 普通:mapBound、level、start/count
174
+ * - 2 视野内 / 10 多边形:mapBound、level
175
+ * - 3 周边:mapBound、level、pointLonlat、queryRadius
176
+ * - 12 行政区划区域:specify
177
+ * - 13 数据分类:specify、dataTypes
178
+ * - 14 统计:specify
179
+ */
180
+ interface SearchPostStr {
181
+ /** 搜索的关键字 */
182
+ keyWord: string;
183
+ /**
184
+ * 查询类型:1 普通(含地铁公交)、2 视野内、3 周边、10 多边形、
185
+ * 12 行政区划区域、13 数据分类、14 统计
186
+ */
187
+ queryType: 1 | 2 | 3 | 10 | 12 | 13 | 14;
188
+ /** 返回结果起始位(0-300),默认 0 */
189
+ start?: number;
190
+ /** 返回结果条数(1-300) */
191
+ count?: number;
192
+ /** 指定行政区国标码(9 位)或名称,如 "156110000" */
193
+ specify?: string;
194
+ /** 地图视野范围 "minx,miny,maxx,maxy"(-180,-90 至 180,90) */
195
+ mapBound?: string;
196
+ /** 目前查询的级别(1-18) */
197
+ level?: number;
198
+ /** 周边搜索中心点坐标 "lng,lat" */
199
+ pointLonlat?: string;
200
+ /** 周边搜索半径 */
201
+ queryRadius?: string;
202
+ /** 多边形搜索顶点串 "lng,lat;lng,lat;..." */
203
+ polygon?: string;
204
+ /** 数据分类(分类名称或编码,多个以英文逗号隔开) */
205
+ dataTypes?: string;
206
+ /** poi 结果信息类别:1 基本信息、2 详细信息 */
207
+ show?: 1 | 2;
208
+ }
209
+ interface SearchResult {
210
+ /** 结果类型:1 普通 POI、2 统计、3 行政区、4 建议词、5 线路 */
211
+ resultType: 1 | 2 | 3 | 4 | 5;
212
+ /** 返回总条数 */
213
+ count: number;
214
+ /** 搜索关键词 */
215
+ keyword: string;
216
+ /** POI 集合(resultType=1) */
217
+ pois?: SearchPoi[];
218
+ /** 统计集合(resultType=2) */
219
+ statistics?: SearchStatistics[];
220
+ /** 行政区集合(resultType=3) */
221
+ area?: SearchArea[];
222
+ /** 建议词(resultType=4;官方类型标注为 json,集合形式待真实环境验证) */
223
+ suggests?: SearchSuggest | SearchSuggest[];
224
+ /** 提示信息(需要提示时返回) */
225
+ prompt?: SearchPrompt;
226
+ /** 线路结果(resultType=5) */
227
+ lineData?: SearchLineData[];
228
+ /** 结果提示信息 */
229
+ status: SearchResultStatus;
230
+ }
231
+ interface SearchPoi {
232
+ /** poi 点名称 */
233
+ name: string;
234
+ /** 电话 */
235
+ phone?: string;
236
+ /** 地址 */
237
+ address?: string;
238
+ /** 坐标 "x,y" */
239
+ lonlat: string;
240
+ /** poi 类型:101 POI 数据、102 公交站点 */
241
+ poiType: 101 | 102;
242
+ /** 英文地址 */
243
+ eaddress?: string;
244
+ /** poi 点英文名称 */
245
+ ename?: string;
246
+ /** poi 热点 ID */
247
+ hotPointID: string;
248
+ /** 所属省名称 */
249
+ province?: string;
250
+ /** 省行政区编码 */
251
+ provinceCode?: string;
252
+ /** 所属城市名称 */
253
+ city?: string;
254
+ /** 市行政区编码 */
255
+ cityCode?: string;
256
+ /** 所属区县名称 */
257
+ county?: string;
258
+ /** 区县行政区编码 */
259
+ countyCode?: string;
260
+ /** 数据信息来源 */
261
+ source: string;
262
+ /** 分类编码 */
263
+ typeCode?: string;
264
+ /** 分类名称 */
265
+ typeName?: string;
266
+ /** 车站信息(poiType=102) */
267
+ stationData?: SearchStationData[];
268
+ /** 公交站 uuid */
269
+ stationUuid?: string;
270
+ }
271
+ interface SearchStationData {
272
+ /** 线路名称 */
273
+ lineName: string;
274
+ /** 线路的 id */
275
+ uuid: string;
276
+ }
277
+ interface SearchStatistics {
278
+ /** 本次统计 POI 总数量 */
279
+ count: number;
280
+ /** 行政区数量 */
281
+ adminCount: number;
282
+ /** 推荐行政区集合 */
283
+ priorityCitys: SearchPriorityCity[];
284
+ /** 各省包含信息集合 */
285
+ allAdmins: SearchAdmin[];
286
+ }
287
+ interface SearchPriorityCity {
288
+ /** 行政区名称 */
289
+ name: string;
290
+ /** 城市数量 */
291
+ count: number;
292
+ /** 行政区经纬度 "x,y" */
293
+ lonlat: string;
294
+ /** 英文行政名称 */
295
+ ename: string;
296
+ /** 城市国标码(9 位) */
297
+ adminCode: number;
298
+ }
299
+ interface SearchAdmin {
300
+ /** 行政名称 */
301
+ name: string;
302
+ /** 包含数量 */
303
+ count: number;
304
+ /** 行政区经纬度 "x,y" */
305
+ lonlat: string;
306
+ /** 省国标码 */
307
+ adminCode: string;
308
+ /** 英文行政名称 */
309
+ ename: string;
310
+ /** 有无下一级行政区:有则 false,无则 true */
311
+ isleaf: boolean;
312
+ }
313
+ interface SearchArea {
314
+ /** 名称 */
315
+ name: string;
316
+ /** 定位范围 "minx,miny,maxx,maxy" */
317
+ bound?: string;
318
+ /** 定位中心点坐标 "x,y" */
319
+ lonlat: string;
320
+ /** 行政区编码 */
321
+ adminCode: number;
322
+ /** 显示级别(1-18) */
323
+ level: number;
324
+ }
325
+ interface SearchSuggest {
326
+ /** 名称 */
327
+ name: string;
328
+ /** 地区地址(可能为空串) */
329
+ address: string;
330
+ /** 国标码 */
331
+ gbCode: string;
332
+ }
333
+ interface SearchPrompt {
334
+ /** 提示类型:1 是否在 where 搜 what、2 在 where 无结果、3 多个可跳转行政区、4 城市 */
335
+ type: 1 | 2 | 3 | 4;
336
+ /** 提示的行政区集合 */
337
+ admins: SearchPromptAdmin[];
338
+ /** 关键字 */
339
+ keyword: string;
340
+ }
341
+ interface SearchPromptAdmin {
342
+ /** 行政区名称 */
343
+ adminName: string;
344
+ /** 行政区划编码 */
345
+ adminCode: string;
346
+ }
347
+ interface SearchLineData {
348
+ /** 站数量 */
349
+ stationNum: string;
350
+ /** 类型,官方标注为 "103" */
351
+ poiType: string;
352
+ /** 线路名称 */
353
+ name: string;
354
+ /** 线路 id */
355
+ uuid: string;
356
+ }
357
+ interface SearchResultStatus {
358
+ /** 服务状态码(1000 正常,2001-2007 参数错误,3000 服务错误,3001 无数据) */
359
+ infocode: number;
360
+ /** 返回中文描述 */
361
+ cndesc: string;
362
+ }
363
+ //#endregion
364
+ //#region src/types/staticImage.d.ts
365
+ /**
366
+ * 静态地图(官方文档 staticapi/static.html)。
367
+ * 端点 /staticimage,返回 PNG 图片本体,可直接作为 <img src>。
368
+ */
369
+ interface StaticImageParams {
370
+ /** 图片宽度(1-1024),默认 400 */
371
+ width?: number;
372
+ /** 图片高度(1-1024),默认 300 */
373
+ height?: number;
374
+ /** 地图中心点 "lng,lat" */
375
+ center?: string;
376
+ /** 地图级别(3-18),默认 10 */
377
+ zoom?: number;
378
+ /** 标注经纬度,多个以 "|" 隔开:"lng,lat|lng,lat" */
379
+ markers?: string;
380
+ /** 标注样式,与 markers 一一对应,多个以 "|" 隔开 */
381
+ markerStyles?: string;
382
+ /** 折线,多条以 "|" 隔开,点间 ";"、坐标 ",":"lng,lat;lng,lat|..." */
383
+ paths?: string;
384
+ /** 折线样式 "color,weight,opacity[,fillColor]",多条以 "|" 隔开 */
385
+ pathStyles?: string;
386
+ /** 叠加图层类型组合,如 "vec_c,cva_c" */
387
+ layers?: string;
388
+ /** 传入经纬度时返回其相对图片左上角的屏幕坐标(优先于覆盖物参数) */
389
+ pixLocation?: string;
390
+ }
391
+ //#endregion
392
+ //#region src/types/transit.d.ts
393
+ /**
394
+ * 公交规划(官方文档 server/bus.html)。
395
+ * 端点 /transit?type=busline,postStr 为 JSON 串。
396
+ */
397
+ /** 公交规划请求:线路规划 / uuid 详情查询 / 站点返程查询 三种形态 */
398
+ type TransitPostStr = {
399
+ /** 出发点坐标 "经度,纬度" */
400
+ startposition: string;
401
+ /** 终点坐标 "经度,纬度" */
402
+ endposition: string;
403
+ /**
404
+ * 规划类型(按位组合):第 0 位较快捷、第 1 位少换乘、
405
+ * 第 2 位少步行、第 3 位不坐地铁
406
+ */
407
+ linetype: string;
408
+ } | {
409
+ /** 公交站或线路的 uuid */
410
+ uuid: string;
411
+ } | {
412
+ /** 线路 ID */
413
+ lineUuid: string;
414
+ /** 站点 ID */
415
+ stationUuid: string;
416
+ };
417
+ interface TransitResult {
418
+ /**
419
+ * 返回数据状态:0 正常、1 找不到起点、2 找不到终点、3 规划失败、
420
+ * 4 起终点 200 米内建议步行、5 起终点 500 米内、6 参数错误
421
+ */
422
+ resultCode: number;
423
+ /** 返回线路中是否包含地铁:0 不包含、1 包含 */
424
+ hasSubway: 0 | 1;
425
+ /** 各请求类型的返回结果(请求几种返回几种) */
426
+ results: TransitTypeResult[];
427
+ }
428
+ interface TransitTypeResult {
429
+ /** 返回线路结果类型(同 linetype 位定义) */
430
+ lineType: number;
431
+ /** 该类型的规划线路(最多 5 条) */
432
+ lines: TransitLine[];
433
+ }
434
+ interface TransitLine {
435
+ /** 单条规划结果的线路名称,如 "3路—4路—5路" */
436
+ lineName: string;
437
+ /** 各段线路信息 */
438
+ segments: TransitSegment[];
439
+ }
440
+ interface TransitSegment {
441
+ /** 线路类型(1-4) */
442
+ segmentType: number;
443
+ /** 起站点 */
444
+ stationStart: TransitStation;
445
+ /** 终站点 */
446
+ stationEnd: TransitStation;
447
+ /** 线路内容 */
448
+ segmentLine: TransitSegmentLine;
449
+ }
450
+ interface TransitStation {
451
+ /** 站点名称 */
452
+ name: string;
453
+ /** 站的 id 信息 */
454
+ uuid: string;
455
+ /** 站点坐标 "x,y" */
456
+ lonlat: string;
457
+ }
458
+ interface TransitSegmentLine {
459
+ /** 此段线路名(不含括号内容) */
460
+ segmentName: string;
461
+ /** 此段线路的完整线路名 */
462
+ direction: string;
463
+ /** 此段线路坐标串 */
464
+ linePoint: string;
465
+ /** 此段距离(米);步行且小于 20 米时不返回 */
466
+ segmentDistance: number;
467
+ /** 此段经过的站点数 */
468
+ segmentStationCount: number;
469
+ /** 此段耗时 */
470
+ segmentTime: number;
471
+ }
472
+ /** uuid 为线路时返回的线路详细信息 */
473
+ interface TransitLineInfo {
474
+ /** 线路名称,如 "68路" */
475
+ lineName: string;
476
+ /** 线路类型:1 公交、2 地铁、3 磁悬浮 */
477
+ lineType: 1 | 2 | 3;
478
+ /** 线路长度(米) */
479
+ length: number;
480
+ /** 站点数据 */
481
+ station: TransitBusStation[];
482
+ /** 线路详细坐标串,点之间空格隔开 */
483
+ linePoint: string;
484
+ /** 始发车时间 "hh:mm"(24 小时制) */
485
+ startTime: string;
486
+ /** 末班车时间 "hh:mm"(24 小时制) */
487
+ endTime: string;
488
+ /** 全程运营总时间(分钟) */
489
+ totalTime: number;
490
+ /** 站点总数量 */
491
+ stationCount: number;
492
+ /** 发车间隔(秒) */
493
+ interval: number;
494
+ /** 计费模式:0 单一、1 按距离、2 按站 */
495
+ ticketcal: 0 | 1 | 2;
496
+ /** 全程票价(分) */
497
+ totalPrice: number;
498
+ /** 起步票价(分) */
499
+ startPrice: number;
500
+ /** 递增距离票价(按千米) */
501
+ increasedPrice: number;
502
+ /** 车站递增票价(按站) */
503
+ increasedStep: number;
504
+ /** 是否支持月票:0 不支持、1 支持 */
505
+ ismonTicket: 0 | 1;
506
+ /** 是否双向行驶:0 单向、1 双向 */
507
+ isBidirectional: 0 | 1;
508
+ /** 是否人工售票:0 有人、1 无人 */
509
+ isManual: 0 | 1;
510
+ /** 状态:0 使用中、1 非使用中 */
511
+ status: 0 | 1;
512
+ /** 所属公交公司 */
513
+ company: string;
514
+ }
515
+ /** uuid 为公交站时返回的站点信息 */
516
+ interface TransitBusStation {
517
+ /** 站点名称 */
518
+ name: string;
519
+ /** 站的 id 信息 */
520
+ uuid: string;
521
+ /** 途经线路信息 */
522
+ linedata: TransitBusLineData[];
523
+ /** 站点坐标 "x,y" */
524
+ lonlat: string;
525
+ }
526
+ interface TransitBusLineData {
527
+ /** 线路名称 */
528
+ name: string;
529
+ /** 线路的 id */
530
+ uuid: string;
531
+ }
532
+ //#endregion
533
+ //#region src/web.d.ts
534
+ declare class TianDiTu {
535
+ tk: string;
536
+ baseURL: string;
537
+ private client;
538
+ constructor(tk: string, baseURL?: string);
539
+ apiFetch<T>(request: FetchRequest, options?: FetchOptions<"json">): Promise<T>;
540
+ /** 地名搜索 V2.0(官方 server/search2.html) */
541
+ search(postStr: SearchPostStr): Promise<SearchResult>;
542
+ /** 驾车规划(官方 server/drive.html) */
543
+ drive(postStr: DrivePostStr): Promise<DriveResult>;
544
+ /** 公交规划 / 公交 uuid 详情查询 / 站点返程查询(官方 server/bus.html) */
545
+ transit(postStr: TransitPostStr): Promise<TransitResult | TransitLineInfo | TransitBusStation>;
546
+ /** 正向地理编码:地址转坐标(官方 server/geocodinginterface.html) */
547
+ geoCoding(ds: GeoCodingDs): Promise<GeoCodingResult>;
548
+ /** 逆地理编码:坐标转地址(官方 server/geocoding.html) */
549
+ reverseGeoCoding(postStr: ReverseGeoCodingPostStr): Promise<ReverseGeoCodingResult>;
550
+ /** 行政区划 V2.0(官方 server/administrative2.html) */
551
+ administrative(params: AdministrativeParams): Promise<AdministrativeResult>;
552
+ /**
553
+ * 静态地图 URL(官方 staticapi/static.html):服务返回 PNG 图片本体,
554
+ * 此处只构造 URL,直接用于 <img src>,不做请求。
555
+ */
556
+ staticImage(params?: StaticImageParams): string;
557
+ }
558
+ //#endregion
559
+ //#region src/index.d.ts
560
+ /**
561
+ * SDK 脚本加载已移交 @tianditu/core 的 loadTdt,本包只负责
562
+ * 天地图 REST 服务的请求封装。
563
+ */
564
+ declare function defineTianditu({ tk, baseURL }: {
565
+ tk: string;
566
+ baseURL?: string;
567
+ }): TianDiTu;
568
+ //#endregion
569
+ export { type AdministrativeCenter, type AdministrativeChild, type AdministrativeData, type AdministrativeDistrict, type AdministrativeParams, type AdministrativeResult, type DrivePostStr, type DriveResult, type GeoCodingDs, type GeoCodingLocation, type GeoCodingResult, type ReverseGeoCodingAddressComponent, type ReverseGeoCodingDetail, type ReverseGeoCodingPoint, type ReverseGeoCodingPostStr, type ReverseGeoCodingResult, type SearchAdmin, type SearchArea, type SearchLineData, type SearchPoi, type SearchPostStr, type SearchPriorityCity, type SearchPrompt, type SearchPromptAdmin, type SearchResult, type SearchResultStatus, type SearchStationData, type SearchStatistics, type SearchSuggest, type StaticImageParams, TianDiTu, type TransitBusLineData, type TransitBusStation, type TransitLine, type TransitLineInfo, type TransitPostStr, type TransitResult, type TransitSegment, type TransitSegmentLine, type TransitStation, type TransitTypeResult, defineTianditu };
package/dist/index.mjs ADDED
@@ -0,0 +1,84 @@
1
+ import { ofetch } from "ofetch";
2
+ //#region src/web.ts
3
+ var TianDiTu = class {
4
+ constructor(tk, baseURL) {
5
+ this.baseURL = "https://api.tianditu.gov.cn";
6
+ this.tk = tk;
7
+ this.baseURL = baseURL || this.baseURL;
8
+ this.client = ofetch.create({ baseURL: this.baseURL });
9
+ }
10
+ apiFetch(request, options) {
11
+ return this.client(request, options);
12
+ }
13
+ /** 地名搜索 V2.0(官方 server/search2.html) */
14
+ async search(postStr) {
15
+ return await this.apiFetch("/v2/search", { params: {
16
+ tk: this.tk,
17
+ type: "query",
18
+ postStr: JSON.stringify(postStr)
19
+ } });
20
+ }
21
+ /** 驾车规划(官方 server/drive.html) */
22
+ async drive(postStr) {
23
+ return await this.apiFetch("/drive", { params: {
24
+ tk: this.tk,
25
+ type: "search",
26
+ postStr: JSON.stringify(postStr)
27
+ } });
28
+ }
29
+ /** 公交规划 / 公交 uuid 详情查询 / 站点返程查询(官方 server/bus.html) */
30
+ async transit(postStr) {
31
+ return await this.apiFetch("/transit", { params: {
32
+ tk: this.tk,
33
+ type: "busline",
34
+ postStr: JSON.stringify(postStr)
35
+ } });
36
+ }
37
+ /** 正向地理编码:地址转坐标(官方 server/geocodinginterface.html) */
38
+ async geoCoding(ds) {
39
+ return await this.apiFetch("/geocoder", { params: {
40
+ tk: this.tk,
41
+ ds: JSON.stringify(ds)
42
+ } });
43
+ }
44
+ /** 逆地理编码:坐标转地址(官方 server/geocoding.html) */
45
+ async reverseGeoCoding(postStr) {
46
+ return await this.apiFetch("/geocoder", { params: {
47
+ tk: this.tk,
48
+ type: "geocode",
49
+ postStr: JSON.stringify({
50
+ ver: 1,
51
+ ...postStr
52
+ })
53
+ } });
54
+ }
55
+ /** 行政区划 V2.0(官方 server/administrative2.html) */
56
+ async administrative(params) {
57
+ return await this.apiFetch("/v2/administrative", { params: {
58
+ tk: this.tk,
59
+ ...params
60
+ } });
61
+ }
62
+ /**
63
+ * 静态地图 URL(官方 staticapi/static.html):服务返回 PNG 图片本体,
64
+ * 此处只构造 URL,直接用于 <img src>,不做请求。
65
+ */
66
+ staticImage(params = {}) {
67
+ const search = new URLSearchParams({
68
+ tk: this.tk,
69
+ ...Object.fromEntries(Object.entries(params).filter(([, value]) => value !== void 0))
70
+ });
71
+ return `${this.baseURL}/staticimage?${search}`;
72
+ }
73
+ };
74
+ //#endregion
75
+ //#region src/index.ts
76
+ /**
77
+ * SDK 脚本加载已移交 @tianditu/core 的 loadTdt,本包只负责
78
+ * 天地图 REST 服务的请求封装。
79
+ */
80
+ function defineTianditu({ tk, baseURL }) {
81
+ return new TianDiTu(tk, baseURL);
82
+ }
83
+ //#endregion
84
+ export { TianDiTu, defineTianditu };
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@tianditu/services",
3
+ "version": "0.1.0",
4
+ "description": "Typed REST client for tianditu services — place search, driving and transit planning, geocoding, administrative divisions, and static images, standalone from the map runtime",
5
+ "keywords": [
6
+ "administrative-divisions",
7
+ "api-client",
8
+ "geocoding",
9
+ "gis",
10
+ "lbs",
11
+ "map",
12
+ "rest",
13
+ "reverse-geocoding",
14
+ "route-planning",
15
+ "search",
16
+ "static-image",
17
+ "tianditu",
18
+ "transit",
19
+ "typescript"
20
+ ],
21
+ "homepage": "https://github.com/DemoMacro/tianditu#readme",
22
+ "bugs": {
23
+ "url": "https://github.com/DemoMacro/tianditu/issues"
24
+ },
25
+ "license": "MIT",
26
+ "author": {
27
+ "name": "Demo Macro",
28
+ "email": "abc@imst.xyz",
29
+ "url": "https://www.demomacro.com/"
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/DemoMacro/tianditu.git"
34
+ },
35
+ "files": [
36
+ "dist"
37
+ ],
38
+ "main": "dist/index.mjs",
39
+ "types": "dist/index.d.mts",
40
+ "dependencies": {
41
+ "ofetch": "1.3.4"
42
+ },
43
+ "scripts": {
44
+ "build": "vp pack"
45
+ }
46
+ }