fix: 瓦片类型仅由搜索结果坐标+蓝点推算,去掉其他判断逻辑

This commit is contained in:
xweiba
2026-08-05 23:15:06 +08:00
parent 3260af748b
commit 794cf9d004
3 changed files with 25 additions and 20 deletions
+12 -1
View File
@@ -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]
+1 -19
View File
@@ -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)
// GCJWGS = 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 {
+12
View File
@@ -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