From 5dd0ef711df6e368002cf27518113fb14b4c84a2 Mon Sep 17 00:00:00 2001 From: xweiba Date: Wed, 5 Aug 2026 22:44:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8E=A8=E7=AE=97=E7=93=A6=E7=89=87?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E2=80=94=E2=80=94GCJ=E2=86=92WGS=E5=90=8E?= =?UTF-8?q?=E6=9B=B4=E6=8E=A5=E8=BF=91=E8=93=9D=E7=82=B9=3DGCJ-02=E7=93=A6?= =?UTF-8?q?=E7=89=87=EF=BC=8C=E5=8F=8D=E4=B9=8B=3DWGS-84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App/MapViewRepresentable.swift | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/App/MapViewRepresentable.swift b/App/MapViewRepresentable.swift index 48ebe84..cc5b5a4 100644 --- a/App/MapViewRepresentable.swift +++ b/App/MapViewRepresentable.swift @@ -260,9 +260,25 @@ struct MapViewRepresentable: UIViewRepresentable { parent.onViewportChanged(distance) zoomLabel?.text = MapZoomMath.viewportScaleLabel(distanceMeters: distance) updatePinPosition(on: mapView) - // 根据地图中心坐标地理位置判断瓦片类型(中国境内=GCJ-02,境外=WGS-84) - let c = mapView.centerCoordinate - CoordinateConverter.updateTileType(lat: c.latitude, lon: c.longitude) + // 推算瓦片类型:比较地图中心与蓝点的偏移 + // 蓝点是 WGS-84,地图中心在 GCJ-02 瓦片上会有 ~596m 偏移 + let mc = mapView.centerCoordinate + if let bl = mapView.userLocation.location?.coordinate { + let wgs = CoordinateConverter.gcj02ToWgs84(lat: mc.latitude, lon: mc.longitude) + let dGCJ = CoordinateConverter.distance(lat1: wgs.lat, lon1: wgs.lon, lat2: bl.latitude, lon2: bl.longitude) + let dRaw = CoordinateConverter.distance(lat1: mc.latitude, lon1: mc.longitude, lat2: bl.latitude, lon2: bl.longitude) + // GCJ→WGS 后更接近蓝点 = 地图是 GCJ-02;原始更接近 = 地图是 WGS-84 + let type: CoordinateConverter.CoordType = dGCJ < dRaw ? .gcj02 : .wgs84 + if type != CoordinateConverter.currentTileType { + CoordinateConverter.currentTileType = type + RuntimeLogger.info("APP", "坐标转换", "推算瓦片 → \(type.rawValue)", details: [ + "dGCJ": String(format: "%.0fm", dGCJ), "dRaw": String(format: "%.0fm", dRaw) + ]) + } + } else { + // 无蓝点,用坐标地理边界兜底 + CoordinateConverter.updateTileType(lat: mc.latitude, lon: mc.longitude) + } // 同步蓝点坐标(避免 delegate 更新不及时导致 mapState.realtimeLocation 为 nil) if let ul = mapView.userLocation.location, CLLocationCoordinate2DIsValid(ul.coordinate), ul.horizontalAccuracy >= 0 {