From 00200cba68f0ee1c6c29444b17e9af8671d20e7e Mon Sep 17 00:00:00 2001 From: lijingjun <349181175@qq.com> Date: Sat, 30 May 2026 09:06:31 +0800 Subject: [PATCH] fix: convert GCJ-02 back to WGS-84 before sending to device, remove China boundary check --- StikJIT/Utilities/CoordinateTransform.swift | 46 ++++++++++++--------- StikJIT/Views/MainTabView.swift | 5 ++- StikJIT/Views/MapSelectionView.swift | 3 +- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/StikJIT/Utilities/CoordinateTransform.swift b/StikJIT/Utilities/CoordinateTransform.swift index 231cadb..a7ca6cf 100644 --- a/StikJIT/Utilities/CoordinateTransform.swift +++ b/StikJIT/Utilities/CoordinateTransform.swift @@ -9,31 +9,39 @@ enum CoordinateTransform { static func wgs84ToGCJ02(_ coordinate: CLLocationCoordinate2D) -> CLLocationCoordinate2D { let lat = coordinate.latitude let lon = coordinate.longitude - - guard outOfChina(lat: lat, lon: lon) else { - let dLat = transformLat(x: lon - 105.0, y: lat - 35.0) - let dLon = transformLon(x: lon - 105.0, y: lat - 35.0) - let radLat = lat / 180.0 * pi - let sinRadLat = sin(radLat) - let magic = 1 - ee * sinRadLat * sinRadLat - let sqrtMagic = sqrt(magic) - let adjustedLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi) - let adjustedLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi) - return CLLocationCoordinate2D( - latitude: lat + adjustedLat, - longitude: lon + adjustedLon - ) - } - - return coordinate + let dLat = transformLat(x: lon - 105.0, y: lat - 35.0) + let dLon = transformLon(x: lon - 105.0, y: lat - 35.0) + let radLat = lat / 180.0 * pi + let sinRadLat = sin(radLat) + let magic = 1 - ee * sinRadLat * sinRadLat + let sqrtMagic = sqrt(magic) + let adjustedLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi) + let adjustedLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi) + return CLLocationCoordinate2D( + latitude: lat + adjustedLat, + longitude: lon + adjustedLon + ) } static func wgs84ToGCJ02(_ coordinates: [CLLocationCoordinate2D]) -> [CLLocationCoordinate2D] { coordinates.map { wgs84ToGCJ02($0) } } - private static func outOfChina(lat: Double, lon: Double) -> Bool { - return !(72.004 <= lon && lon <= 137.8347 && 0.8293 <= lat && lat <= 55.8271) + static func gcj02ToWGS84(_ coordinate: CLLocationCoordinate2D) -> CLLocationCoordinate2D { + let gcjLat = coordinate.latitude + let gcjLon = coordinate.longitude + let dLat = transformLat(x: gcjLon - 105.0, y: gcjLat - 35.0) + let dLon = transformLon(x: gcjLon - 105.0, y: gcjLat - 35.0) + let radLat = gcjLat / 180.0 * pi + let sinRadLat = sin(radLat) + let magic = 1 - ee * sinRadLat * sinRadLat + let sqrtMagic = sqrt(magic) + let adjustedLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi) + let adjustedLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi) + return CLLocationCoordinate2D( + latitude: gcjLat - adjustedLat, + longitude: gcjLon - adjustedLon + ) } private static func transformLat(x: Double, y: Double) -> Double { diff --git a/StikJIT/Views/MainTabView.swift b/StikJIT/Views/MainTabView.swift index 81f85e9..eecadc8 100644 --- a/StikJIT/Views/MainTabView.swift +++ b/StikJIT/Views/MainTabView.swift @@ -134,10 +134,11 @@ struct MainTabView: View { } LocationSimulationCommandQueue.shared.async { + let wgs84 = CoordinateTransform.gcj02ToWGS84(coordinate) let code = simulate_location( DeviceConnectionContext.targetIPAddress, - coordinate.latitude, - coordinate.longitude, + wgs84.latitude, + wgs84.longitude, pairingFile.path ) diff --git a/StikJIT/Views/MapSelectionView.swift b/StikJIT/Views/MapSelectionView.swift index e829321..eff6f5f 100644 --- a/StikJIT/Views/MapSelectionView.swift +++ b/StikJIT/Views/MapSelectionView.swift @@ -1571,7 +1571,8 @@ struct LocationSimulationView: View { } private func locationUpdateCode(for coordinate: CLLocationCoordinate2D) -> Int32 { - simulate_location(deviceIP, coordinate.latitude, coordinate.longitude, pairingFilePath) + let wgs84 = CoordinateTransform.gcj02ToWGS84(coordinate) + simulate_location(deviceIP, wgs84.latitude, wgs84.longitude, pairingFilePath) } }