KKWLocationService.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // KKWLocationService.swift
  3. // SolarLamp
  4. //
  5. // Created by liaolj on 16/11/20.
  6. // Copyright © 2016年 SolarLamp. All rights reserved.
  7. //
  8. import UIKit
  9. import MapKit
  10. class KKWLocationService: NSObject {
  11. var locationService: CLLocationManager!
  12. typealias coordinationCallBack = ((CLLocationCoordinate2D)->())?
  13. var callBack:coordinationCallBack!
  14. typealias currentCityCallBack = ((NSString)->())?
  15. var cityCallBack :currentCityCallBack!
  16. static let shareInstance = KKWLocationService()
  17. override init() {
  18. super.init()
  19. locationService = CLLocationManager()
  20. locationService.delegate = self
  21. //设置定位精确度
  22. locationService.desiredAccuracy = kCLLocationAccuracyBest
  23. //设置定位请求状态
  24. if #available(iOS 8.0, *) {
  25. locationService.requestWhenInUseAuthorization()
  26. }else {
  27. print("定位失败")
  28. }
  29. if #available(iOS 9.0, *) {
  30. locationService.allowsBackgroundLocationUpdates = false
  31. } else {
  32. // Fallback on earlier versions
  33. }
  34. // locationService.delegate = self
  35. if CLLocationManager.locationServicesEnabled() {
  36. start()
  37. }else {
  38. print("没有定位服务")
  39. }
  40. }
  41. func start() {
  42. locationService.startUpdatingLocation()
  43. }
  44. //
  45. // func didUpdateBMKUserLocation(userLocation: BMKUserLocation!) {
  46. // //print("didUpdateUserLocation lat:\(userLocation.location.coordinate.latitude) lon:\(userLocation.location.coordinate.longitude)")
  47. //// let coordinate = userLocation.location.coordinate
  48. //// callBack?(coordinate)
  49. //// locationService.stopUserLocationService()
  50. // }
  51. }
  52. extension KKWLocationService: CLLocationManagerDelegate {
  53. func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
  54. print(error)
  55. }
  56. //定位更新地理信息调用的代理方法
  57. func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  58. // print()
  59. if let coordinate = manager.location?.coordinate {
  60. //判断是不是属于国内范围 在之外
  61. if EMCoordinateTransform.isLocationOut(ofChina: coordinate){
  62. callBack?(coordinate)
  63. }else{
  64. let coord = EMCoordinateTransform.transformFromWGS(toGCJ: coordinate)
  65. callBack?(coord)
  66. }
  67. }
  68. if locations.count > 0 {
  69. let location = locations.last
  70. let clGeoCoder = CLGeocoder()
  71. let cl = CLLocation(latitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!)
  72. clGeoCoder.reverseGeocodeLocation(cl) { (placemarks, error) in
  73. if placemarks != nil {
  74. let plackmark = placemarks?.last
  75. let addressDic:NSDictionary = plackmark!.addressDictionary as! [String:AnyObject] as NSDictionary
  76. let City:NSString = addressDic.object(forKey: "City") as! NSString
  77. print("当前城市 --\(City)")
  78. // self.cityCallBack?(City)
  79. }
  80. }
  81. }
  82. manager.stopUpdatingLocation()
  83. }
  84. }
  85. // WGS04 To GCJ02(火星坐标)
  86. extension KKWLocationService{
  87. func transformFromWGSToGCJ(wgsLoc:CLLocationCoordinate2D) -> CLLocationCoordinate2D {
  88. let a = 6378245.0;
  89. let ee = 0.00669342162296594323;
  90. let pi = Double.pi
  91. var adjustLoc :CLLocationCoordinate2D?
  92. if isLocationOutChina(location: wgsLoc) { //在国外
  93. adjustLoc = wgsLoc
  94. } else {
  95. var adjustLat = transformLatitude(x: wgsLoc.longitude - 105.0, y: wgsLoc.latitude - 35.0)
  96. var adjustLon = transformLongitude(x: wgsLoc.longitude - 105.0, y: wgsLoc.latitude - 35.0)
  97. let radLat = wgsLoc.latitude / 180.0 * pi;
  98. var magic = sin(radLat);
  99. magic = 1 - ee * magic * magic;
  100. let sqrtMagic = sqrt(magic);
  101. adjustLat = (adjustLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
  102. adjustLon = (adjustLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
  103. adjustLoc!.latitude = wgsLoc.latitude + adjustLat;
  104. adjustLoc!.longitude = wgsLoc.longitude + adjustLon;
  105. }
  106. return adjustLoc!
  107. }
  108. //判断是不是在中国
  109. func isLocationOutChina(location:CLLocationCoordinate2D) -> Bool {
  110. if location.longitude < 72.004 || location.longitude > 137.8347 || location.latitude < 0.8293 || location.latitude > 55.8271 {
  111. return true
  112. }
  113. return false
  114. }
  115. //转纬度
  116. func transformLatitude(x : Double, y :Double) -> Double {
  117. let pi = Double.pi
  118. var lat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x))
  119. lat += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0
  120. lat += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0
  121. lat += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0
  122. return lat
  123. }
  124. //转经度
  125. func transformLongitude(x:Double, y:Double) -> Double {
  126. let pi = Double.pi
  127. var lon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x));
  128. lon += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
  129. lon += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0;
  130. lon += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0;
  131. return lon;
  132. }
  133. }