1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- import UIKit
- import CoreLocation
- let a = 6378245.0
- let ee = 0.00669342162296594323
- extension CLLocation {
- public func transformationWorldCoordinateToMars() -> CLLocationCoordinate2D {
- let lat = coordinate.latitude
- let lon = coordinate.longitude
- if outOfChina() {
- return coordinate
- }
- var dLat = transformLatWithX(x: lon - 105.0, y: lat - 35.0)
- var dLon = transformLonWithY(x: lon - 105.0, y: lat - 35.0)
- let radLat = lat / 180.0 * .pi
- var magic = sin(radLat)
- magic = 1 - ee * magic * magic
- let sqrtMagic = sqrt(magic)
- dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * .pi)
- dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * .pi)
- return CLLocationCoordinate2DMake(dLat + coordinate.latitude, dLon + coordinate.longitude)
- }
-
-
- public func outOfChina() -> Bool {
- guard case 72.005..<137.8347 = coordinate.longitude else{
- return true
- }
- guard case 0.8293..<55.8271 = coordinate.latitude else{
- return true
- }
- return false
- }
-
- public func transformLatWithX(x: Double, y: Double) -> Double {
- var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x))
- ret += (20.0 * sin(6.0 * x * .pi) + 20.0 * sin(2.0 * x * .pi)) * 2.0 / 3.0
- ret += (20.0 * sin(y * .pi) + 40.0 * sin(y / 3.0 * .pi)) * 2.0 / 3.0
- ret += (160.0 * sin(y / 12.0 * .pi) + 320.0 * sin(y * .pi / 30.0)) * 2.0 / 3.0
- return ret
- }
-
- public func transformLonWithY(x: Double, y: Double) -> Double {
- var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x))
- ret += (20.0 * sin(6.0 * x * .pi) + 20.0 * sin(2.0 * x * .pi)) * 2.0 / 3.0
- ret += (20.0 * sin(x * .pi) + 40.0 * sin(x / 3.0 * .pi)) * 2.0 / 3.0
- ret += (150.0 * sin(x / 12.0 * .pi) + 300.0 * sin(x / 30.0 * .pi)) * 2.0 / 3.0
- return ret
- }
- }
|