From 794cf9d004aad0e60241174d84f673d60b822a55 Mon Sep 17 00:00:00 2001 From: xweiba Date: Wed, 5 Aug 2026 23:15:06 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=93=A6=E7=89=87=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E4=BB=85=E7=94=B1=E6=90=9C=E7=B4=A2=E7=BB=93=E6=9E=9C=E5=9D=90?= =?UTF-8?q?=E6=A0=87+=E8=93=9D=E7=82=B9=E6=8E=A8=E7=AE=97=EF=BC=8C?= =?UTF-8?q?=E5=8E=BB=E6=8E=89=E5=85=B6=E4=BB=96=E5=88=A4=E6=96=AD=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App/MapHomeView.swift | 13 ++++++++++++- App/MapViewRepresentable.swift | 20 +------------------- Shared/CoordinateConverter.swift | 12 ++++++++++++ 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/App/MapHomeView.swift b/App/MapHomeView.swift index 097aeb8..bee6f54 100644 --- a/App/MapHomeView.swift +++ b/App/MapHomeView.swift @@ -795,7 +795,18 @@ struct MapHomeView: View { searchError = error.localizedDescription return } - searchResults = (response?.mapItems ?? []).prefix(6).map { item in + let items = (response?.mapItems ?? []).prefix(6) + // 用首个搜索结果坐标推算瓦片类型 + if let first = items.first { + let c = first.placemark.coordinate + if let newType = CoordinateConverter.detectTile( + resultCoord: (c.latitude, c.longitude), + userLocation: mapState.realtimeLocation + ) { + CoordinateConverter.currentTileType = newType + } + } + searchResults = items.map { item in let r = SearchLocationResult( name: item.name ?? "未命名", subtitle: [item.placemark.locality, item.placemark.subLocality, item.placemark.thoroughfare] diff --git a/App/MapViewRepresentable.swift b/App/MapViewRepresentable.swift index cc5b5a4..e94e5a7 100644 --- a/App/MapViewRepresentable.swift +++ b/App/MapViewRepresentable.swift @@ -260,25 +260,7 @@ struct MapViewRepresentable: UIViewRepresentable { parent.onViewportChanged(distance) zoomLabel?.text = MapZoomMath.viewportScaleLabel(distanceMeters: distance) updatePinPosition(on: mapView) - // 推算瓦片类型:比较地图中心与蓝点的偏移 - // 蓝点是 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 { diff --git a/Shared/CoordinateConverter.swift b/Shared/CoordinateConverter.swift index 98730c5..af5fcc8 100644 --- a/Shared/CoordinateConverter.swift +++ b/Shared/CoordinateConverter.swift @@ -1,4 +1,5 @@ import Foundation +import CoreLocation /// GCJ-02 (火星坐标) ↔ WGS-84 坐标转换。 /// @@ -95,6 +96,17 @@ enum CoordinateConverter { return .wgs84 } + /// 通过搜索结果坐标 + 蓝点推算瓦片类型(搜索结果在当期瓦片坐标系,蓝点是 WGS-84) + static func detectTile(resultCoord: (lat: Double, lon: Double), userLocation: CLLocation?) -> CoordType? { + guard let bl = userLocation, + CLLocationCoordinate2DIsValid(bl.coordinate), + bl.horizontalAccuracy >= 0 else { return nil } + let wgs = gcj02ToWgs84(lat: resultCoord.lat, lon: resultCoord.lon) + let dGCJ = distance(lat1: wgs.lat, lon1: wgs.lon, lat2: bl.coordinate.latitude, lon2: bl.coordinate.longitude) + let dRaw = distance(lat1: resultCoord.lat, lon1: resultCoord.lon, lat2: bl.coordinate.latitude, lon2: bl.coordinate.longitude) + return dGCJ < dRaw ? .gcj02 : .wgs84 + } + // MARK: - 核心转换 /// GCJ-02 → WGS-84(迭代法,精度优于 0.5 米)