refactor(Phase 0): 拆出 src/core/geo.js(坐标转换 + 距离)

- 新增 static/js/src/core/geo.js:offsetCoord(GCJ02->BD09, coordtransform 优先、
  常量偏移兜底) 与 calcDistance(Haversine),经典脚本 IIFE 形式
- 同时把 offsetCoord/calcDistance 挂回 window:map.js 里 19+7 处调用与
  136 处内联事件都不用改,风险最低
- 从 map.js 删除重复定义:顶层 offsetCoord、顶层 calcDistance、
  findNearestBike 内嵌的那份 calcDistance(原来同名函数定义了两遍)
- map.html 在 map.js 之前引入(defer 顺序),MAP_ASSETS 纳入版本哈希

map.js 5017 -> 4708 行。浏览器实测:Geo/offsetCoord/calcDistance 均可用;
已知两点距离 24m(与 Haversine 一致);BD09 偏移 dLat 0.00617/dLat 0.00645
与改造前一致;卡片「步行 301 米 · 约 4 分钟」正常;498 辆车、JS 异常 0
测试:167 pytest + 32 node 全绿
This commit is contained in:
zzy
2026-11-16 10:07:04 +08:00
parent be016621c0
commit 30592cc07d
5 changed files with 77 additions and 42 deletions
-40
View File
@@ -124,10 +124,6 @@
const RENTAL_KEY = "xbniu_rental_key"; // localStorage key
// 手动坐标偏移(仅作为 coordtransform 未加载时的降级常量;正常走 gcj02tobd09 自动转换)
const COORD_OFFSET = {
lat: 0.00603, // 约610米(纬度)
lng: 0.00650, // 约600米-20米(经度,往西20米)
};
// 初始位置额外偏移(再往上30米)
const INIT_OFFSET_EXTRA = 0.00027; // 约30米(向北)
@@ -135,23 +131,6 @@
// 坐标转换:车辆/地图数据为 GCJ02,逐点精确转百度 BD09(coordtransform,无在线转换上限)。
// coordtransform.gcj02tobd09(lng, lat) -> [bd09_lng, bd09_lat]
function offsetCoord(lat, lng) {
lat = parseFloat(lat);
lng = parseFloat(lng);
if (typeof coordtransform !== "undefined" && coordtransform.gcj02tobd09) {
try {
const bd = coordtransform.gcj02tobd09(lng, lat); // [lng, lat]
return { lat: bd[1], lng: bd[0] };
} catch (e) {
console.warn("[坐标转换] gcj02tobd09 失败,回退手动偏移:", e);
}
}
// 降级:coordtransform 未加载/出错时,沿用旧的手动常量偏移,避免点位整体偏差
return {
lat: lat + COORD_OFFSET.lat,
lng: lng + COORD_OFFSET.lng,
};
}
// 状态判断函数 - 基于 state + status 组合判断
// state=1 + status=0: 空闲可用
@@ -1264,16 +1243,6 @@
}
// 计算两点之间的距离(米)
function calcDistance(lat1, lng1, lat2, lng2) {
const R = 6371000; // 地球半径(米)
const dLat = (lat2 - lat1) * Math.PI / 180;
const dLng = (lng2 - lng1) * Math.PI / 180;
const a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
Math.sin(dLng/2) * Math.sin(dLng/2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return R * c;
}
// 导航相关已拆到 static/js/navigation.js
@@ -4819,15 +4788,6 @@
// 计算距离并找最近可用车辆(智能电量匹配)
function findNearestBike(myPoint) {
// Haversine公式计算两点距离(米)
function calcDistance(lat1, lng1, lat2, lng2) {
const R = 6371000;
const dLat = (lat2 - lat1) * Math.PI / 180;
const dLng = (lng2 - lng1) * Math.PI / 180;
const a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
Math.sin(dLng/2) * Math.sin(dLng/2);
return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
}
const myLat = myPoint.lat;
const myLng = myPoint.lng;