forked from Yu9191/wloc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
46 lines (41 loc) · 2.02 KB
/
Copy pathindex.js
File metadata and controls
46 lines (41 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { Hono } from "hono/tiny";
import { getPageHtml } from "./page.js";
import { parseCoords, gcj02ToWgs84, toWgs84, round6, inRange } from "./parse.js";
const app = new Hono();
app.get("/", (c) => {
return c.html(getPageHtml());
});
// 地图链接解析: 供快捷指令调用。
// GET /api/parse?u=<链接>&format=json&cs=<gcj|none>
// 返回 {lat, lon, name}; 高德/苹果地图(中国大陆均为 GCJ-02)自动转 WGS84; 境外坐标自动跳过(out_of_china)。cs=none 可强制不转换。
// 不带 format=json 时返回纯文本 "lat=..&lon=.." 片段。
app.get("/api/parse", async (c) => {
const raw = c.req.query("u") || "";
const cs = (c.req.query("cs") || "").toLowerCase();
const fmt = (c.req.query("format") || "").toLowerCase();
try {
let { lat, lon, name, src } = await parseCoords(raw);
// 默认按来源自动换算; cs=none 强制不转换, cs=gcj/bd 强制按指定坐标系转换。
if (cs === "gcj") ({ lat, lon } = gcj02ToWgs84(lat, lon));
else if (cs === "bd") ({ lat, lon } = toWgs84(lat, lon, "baidu"));
else if (cs !== "none") ({ lat, lon } = toWgs84(lat, lon, src));
// 出口再校验一次: cs= 是调用方指定的, 强行按错误坐标系换算也可能把值推出值域。
// 宁可报错也不要返回一个能被当成坐标写进设备的数字。
if (!inRange(lat, lon)) throw new Error("解析出的坐标超出合法范围");
lat = round6(lat);
lon = round6(lon);
name = name || "";
c.header("Access-Control-Allow-Origin", "*");
if (fmt === "json") return c.json({ lat, lon, name });
return c.text(`lat=${lat}&lon=${lon}`);
} catch (e) {
c.header("Access-Control-Allow-Origin", "*");
return c.json({ error: String(e && e.message ? e.message : e) }, 422);
}
});
// 兜底 500 也要带 CORS —— 否则快捷指令那边看到的是跨域错误, 而不是真正的原因。
app.onError((e, c) => {
c.header("Access-Control-Allow-Origin", "*");
return c.text(`${e && e.message ? e.message : e}`, 500);
});
export default app;