From 1a332fc0e180e643584b4cec80b9ecec6d0f780b Mon Sep 17 00:00:00 2001 From: TongTongStudio Date: Sun, 29 Nov 2026 08:02:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(map):=20=E8=AE=BE=E5=A4=87=E5=AE=9A?= =?UTF-8?q?=E4=BD=8D=E6=8C=89=20BD-09=20=E8=BD=AC=20GCJ-02?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 设备端百度定位 SDK 以 bd09ll 上报、后端原样存储,原先按 WGS84 转换会二次偏移。utils/coord.ts 新增 bd09ToGcj02,首页与地图详情改用它;AGENTS.md 同步坐标约定。 --- AGENTS.md | 4 ++-- pages/home/index.ts | 8 ++++---- pages/map/index.ts | 6 +++--- utils/coord.ts | 27 +++++++++++++++++++++++---- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 0d404c3..60b35fd 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -47,7 +47,7 @@ - 地图一律用**微信原生 ``**(底图为微信内置的腾讯位置服务),**不接入百度底图**:小程序原生 map 无法显示百度瓦片,`web-view` 嵌百度 JS API 的方案不采用(体验差、需商业授权)。 - 只做展示时**不需要任何 KEY/SDK**;个性化样式或腾讯地图服务 API 才需要腾讯 `subkey`,届时再单独申请。 -- **坐标系**:`` 只认 **GCJ-02**,而后端设备定位与 Flutter 端按 **WGS84** 处理。落图前必须调用 `utils/coord.ts` 的 `wgs84ToGcj02(lng, lat)`,不要把 WGS84 直接塞给 map(会偏移数百米)。 +- **坐标系**:`` 与 `wx.openLocation` 只认 **GCJ-02**;设备端百度定位 SDK 以 `setCoorType("bd09ll")` 上报,后端原样存储,所以 `/client/sn/location` 返回的是 **BD-09**。落图前必须调用 `utils/coord.ts` 的 `bd09ToGcj02(lng, lat)`,**不要**把 BD-09 当成 WGS84 调 `wgs84ToGcj02`,会二次偏移。后台管理页用百度 JS API 直接吃 BD-09 是正确的,不要改它。 - 首页地图是"预览 + 点击进详情":map 上覆盖 `.map-mask` 承接点击并屏蔽手势,`enable-scroll/zoom/rotate/overlooking` 全部为 `false`。 - marker 图标为 `assets/marker.png`(48×48 绿点白边,显示 24px);新增地图点位图标放 `assets/`。 - 后续若需要 POI 检索/路线规划,优先评估百度小程序 JSAPI(`BMapWX`,仅数据服务、返回 GCJ-02 数据,AK 需配 `api.map.baidu.com` 请求域名)或腾讯位置服务(与底图同源),二选一,不要同时引两套。 @@ -106,7 +106,7 @@ - 工程已完成 TypeScript 化,`npm run typecheck` 与开发者工具编译均通过。 - 底部 3 Tab 已可用(自定义 tabBar,点击切换)。 -- **首页已接入真实数据**:设备列表/在线状态取 `/api/v1/client/sn/my-devices`,实时位置取 `/client/sn/location`(WGS84→GCJ-02 后落图),最近截图取 `/client/sn/upload-screenshot`(`` 直接加载对象存储 URL,可点击预览)。今日使用时长仍为静态演示——后端暂无对应接口。 +- **首页已接入真实数据**:设备列表/在线状态取 `/api/v1/client/sn/my-devices`,实时位置取 `/client/sn/location`(BD-09→GCJ-02 后落图),最近截图取 `/client/sn/upload-screenshot`(`` 直接加载对象存储 URL,可点击预览)。今日使用时长仍为静态演示——后端暂无对应接口。 - **登录已接入**:`pages/login` 提供手机号验证码登录(自动注册)与微信登录(`getPhoneNumber` 一键登录 / 静默登录),令牌存本地,401 自动刷新;Tab 页 `onShow` 用 `ensureLogin()` 兜底跳转。 - **我的页已接资料**:`GET /client/account/profile` 显示昵称/签名/头像,提供退出登录;其余设置项仍 `showTodo`。 - 管理页仍为占位(9 个待迁移模块)。 diff --git a/pages/home/index.ts b/pages/home/index.ts index 901dd9a..b0e3815 100644 --- a/pages/home/index.ts +++ b/pages/home/index.ts @@ -16,7 +16,7 @@ import { } from '../../utils/api' import { ensureLogin } from '../../utils/auth' import { getDeviceSn, saveDeviceSn } from '../../utils/token' -import { wgs84ToGcj02 } from '../../utils/coord' +import { bd09ToGcj02 } from '../../utils/coord' import { showTodo } from '../../utils/todo' import { syncTabBar } from '../../utils/tab-bar' import { DeviceStatusSseClient } from '../../utils/sse' @@ -303,9 +303,9 @@ Page({ this.setData({ screenshots: withShotPlaceholders(items) }) }, - /** 后端下发的是 WGS84,微信 map 只认 GCJ-02,落图前必须转换,否则偏移数百米。 */ - applyDeviceLocation(wgs: { latitude: number; longitude: number }) { - const gcj = wgs84ToGcj02(wgs.longitude, wgs.latitude) + /** 设备端上报 BD-09,微信 map 只认 GCJ-02,落图前必须转换,否则偏移数百米。 */ + applyDeviceLocation(bd09: { latitude: number; longitude: number }) { + const gcj = bd09ToGcj02(bd09.longitude, bd09.latitude) const markers: MapMarker[] = [ { id: 1, diff --git a/pages/map/index.ts b/pages/map/index.ts index b3b0b00..8b3d07b 100644 --- a/pages/map/index.ts +++ b/pages/map/index.ts @@ -1,7 +1,7 @@ import type { DeviceLocation } from '../../utils/api' import { getDeviceLocation } from '../../utils/api' import { getDeviceSn } from '../../utils/token' -import { wgs84ToGcj02 } from '../../utils/coord' +import { bd09ToGcj02 } from '../../utils/coord' interface MapMarker { id: number @@ -66,8 +66,8 @@ Page({ return } - // 后端上报 WGS84,微信 map 只认 GCJ-02 - const gcj = wgs84ToGcj02(longitude, latitude) + // 设备端百度定位上报 BD-09,微信 map/openLocation 只认 GCJ-02 + const gcj = bd09ToGcj02(longitude, latitude) const detail = [ location.province, location.city, diff --git a/utils/coord.ts b/utils/coord.ts index a591554..5772819 100644 --- a/utils/coord.ts +++ b/utils/coord.ts @@ -1,8 +1,10 @@ /** * 坐标转换工具。 * - * 微信原生 组件使用 GCJ-02(火星坐标系),而后端设备定位与 Flutter 端 - * 统一按 WGS84 处理,直接叠加会在国内偏移 300~600 米,因此打点前必须转换。 + * 设备端百度定位 SDK 以 `setCoorType("bd09ll")` 上报,后端原样存储, + * 因此 /api/v1/client/sn/location 返回的是 BD-09 坐标。 + * 微信原生 与 wx.openLocation 只认 GCJ-02(火星坐标系), + * 直接把 BD-09 塞进去会偏移数百米,落图前必须调用 bd09ToGcj02。 */ const PI = Math.PI const A = 6378245.0 @@ -13,7 +15,7 @@ export interface LatLng { longitude: number } -/** 国外坐标不做偏移(GCJ-02 只在国内生效)。 */ +/** 国外坐标不做偏移(国内坐标系只在国内生效)。 */ function outOfChina(lng: number, lat: number): boolean { return lng < 72.004 || lng > 137.8347 || lat < 0.8293 || lat > 55.8271 } @@ -46,7 +48,24 @@ function transformLng(x: number, y: number): number { return ret } -/** WGS84 → GCJ-02。 */ +/** BD-09(百度经纬度)→ GCJ-02:先去掉百度偏移,再做边界判定。 */ +export function bd09ToGcj02(lng: number, lat: number): LatLng { + const x = lng - 0.0065 + const y = lat - 0.006 + const z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * PI) + const theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * PI) + const gcjLng = z * Math.cos(theta) + const gcjLat = z * Math.sin(theta) + if (outOfChina(gcjLng, gcjLat)) { + return { latitude: lat, longitude: lng } + } + return { latitude: gcjLat, longitude: gcjLng } +} + +/** + * WGS84 → GCJ-02(保留给将来可能出现的 WGS84 来源数据)。 + * 当前后端设备定位是 BD-09,不要用它处理设备坐标。 + */ export function wgs84ToGcj02(lng: number, lat: number): LatLng { if (outOfChina(lng, lat)) { return { latitude: lat, longitude: lng }