No Description

CLLocationExt.swift 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // CLLocationExt.swift
  3. // ExtensionKit
  4. //
  5. // Created by FFIB on 2017/9/24.
  6. // Copyright © 2017年 FFIB. All rights reserved.
  7. //
  8. import UIKit
  9. import CoreLocation
  10. let a = 6378245.0
  11. let ee = 0.00669342162296594323
  12. //the world coordinate transformation to mars
  13. extension CLLocation {
  14. public func transformationWorldCoordinateToMars() -> CLLocationCoordinate2D {
  15. let lat = coordinate.latitude
  16. let lon = coordinate.longitude
  17. if outOfChina() {
  18. return coordinate
  19. }
  20. var dLat = transformLatWithX(x: lon - 105.0, y: lat - 35.0)
  21. var dLon = transformLonWithY(x: lon - 105.0, y: lat - 35.0)
  22. let radLat = lat / 180.0 * .pi
  23. var magic = sin(radLat)
  24. magic = 1 - ee * magic * magic
  25. let sqrtMagic = sqrt(magic)
  26. dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * .pi)
  27. dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * .pi)
  28. return CLLocationCoordinate2DMake(dLat + coordinate.latitude, dLon + coordinate.longitude)
  29. }
  30. //judge china
  31. public func outOfChina() -> Bool {
  32. guard case 72.005..<137.8347 = coordinate.longitude else{
  33. return true
  34. }
  35. guard case 0.8293..<55.8271 = coordinate.latitude else{
  36. return true
  37. }
  38. return false
  39. }
  40. public func transformLatWithX(x: Double, y: Double) -> Double {
  41. var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x))
  42. ret += (20.0 * sin(6.0 * x * .pi) + 20.0 * sin(2.0 * x * .pi)) * 2.0 / 3.0
  43. ret += (20.0 * sin(y * .pi) + 40.0 * sin(y / 3.0 * .pi)) * 2.0 / 3.0
  44. ret += (160.0 * sin(y / 12.0 * .pi) + 320.0 * sin(y * .pi / 30.0)) * 2.0 / 3.0
  45. return ret
  46. }
  47. public func transformLonWithY(x: Double, y: Double) -> Double {
  48. var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x))
  49. ret += (20.0 * sin(6.0 * x * .pi) + 20.0 * sin(2.0 * x * .pi)) * 2.0 / 3.0
  50. ret += (20.0 * sin(x * .pi) + 40.0 * sin(x / 3.0 * .pi)) * 2.0 / 3.0
  51. ret += (150.0 * sin(x / 12.0 * .pi) + 300.0 * sin(x / 30.0 * .pi)) * 2.0 / 3.0
  52. return ret
  53. }
  54. }