BaiduMapManager.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // BaiduMapManager.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2018/10/17.
  6. // Copyright © 2018 南鑫林. All rights reserved.
  7. //
  8. import UIKit
  9. public class BaiduMapManager: NSObject {
  10. private static let _sharedInstance = BaiduMapManager()
  11. private override init() {} // 私有化init方法
  12. class func shared() -> BaiduMapManager {
  13. return _sharedInstance
  14. }
  15. typealias LocationModelBlock = (_ locationModel: LocationModel) -> Void
  16. var locationModel = LocationModel.shared().getLocationModel()
  17. public func initBaiduMap() -> Void {
  18. BMKLocationAuth.sharedInstance()?.checkPermision(withKey: kBaiduMapAppKey, authDelegate: self)
  19. initLocation()
  20. }
  21. /// 单次定位
  22. func initLocation() {
  23. locationManager.requestLocation(withReGeocode: true, withNetworkState: true) { [weak self] location, state, error in
  24. if (error != nil) {
  25. LocationModel.shared().setLocationModel(locationModel: (self?.locationModel!)!)
  26. return
  27. } else {
  28. if let location = location {
  29. if let location = location.location {
  30. self?.locationModel?.latitude = String(describing: location.coordinate.latitude)
  31. self?.locationModel?.longitude = String(describing: location.coordinate.longitude)
  32. }
  33. if let rgcData = location.rgcData {
  34. self?.locationModel?.cityCode = rgcData.cityCode ?? ""
  35. self?.locationModel?.province = rgcData.province ?? ""
  36. self?.locationModel?.city = rgcData.city ?? ""
  37. self?.locationModel?.district = rgcData.district ?? ""
  38. self?.locationModel?.street = rgcData.street ?? ""
  39. self?.locationModel?.locationdescribe = rgcData.locationDescribe ?? ""
  40. }
  41. LocationModel.shared().setLocationModel(locationModel: (self?.locationModel!)!)
  42. }
  43. }
  44. self?.locationModel = LocationModel.shared().getLocationModel()
  45. }
  46. }
  47. /// 单次定位
  48. func initLocation(locationModelBlock:@escaping LocationModelBlock) -> Void {
  49. locationManager.requestLocation(withReGeocode: true, withNetworkState: true) { [weak self] location, state, error in
  50. if (error != nil) {
  51. LocationModel.shared().setLocationModel(locationModel:(self?.locationModel!)!)
  52. return
  53. } else {
  54. if let location = location {
  55. if let location = location.location {
  56. self?.locationModel?.latitude = String(describing: location.coordinate.latitude)
  57. self?.locationModel?.longitude = String(describing: location.coordinate.longitude)
  58. }
  59. if let rgcData = location.rgcData {
  60. self?.locationModel?.cityCode = rgcData.cityCode ?? ""
  61. self?.locationModel?.province = rgcData.province ?? ""
  62. self?.locationModel?.city = rgcData.city ?? ""
  63. self?.locationModel?.district = rgcData.district ?? ""
  64. self?.locationModel?.street = rgcData.street ?? ""
  65. self?.locationModel?.locationdescribe = rgcData.locationDescribe ?? ""
  66. }
  67. LocationModel.shared().setLocationModel(locationModel: (self?.locationModel!)!)
  68. }
  69. }
  70. self?.locationModel = LocationModel.shared().getLocationModel()
  71. locationModelBlock((self?.locationModel!)!)
  72. }
  73. }
  74. private lazy var locationManager: BMKLocationManager = {
  75. let locationManager = BMKLocationManager.init()
  76. //设置delegate
  77. locationManager.delegate = self
  78. //设置返回位置的坐标系类型
  79. locationManager.coordinateType = BMKLocationCoordinateType.BMK09LL
  80. //设置距离过滤参数
  81. locationManager.distanceFilter = kCLDistanceFilterNone
  82. //设置预期精度参数
  83. locationManager.desiredAccuracy = kCLLocationAccuracyBest
  84. //设置应用位置类型
  85. locationManager.activityType = CLActivityType.automotiveNavigation
  86. //设置是否自动停止位置更新
  87. locationManager.pausesLocationUpdatesAutomatically = false
  88. //设置是否允许后台定位
  89. locationManager.allowsBackgroundLocationUpdates = true
  90. //设置位置获取超时时间
  91. locationManager.locationTimeout = 10
  92. //设置获取地址信息超时时间
  93. locationManager.reGeocodeTimeout = 10
  94. return locationManager
  95. }()
  96. }
  97. // MARK: - 定位代理
  98. extension BaiduMapManager:BMKLocationManagerDelegate {
  99. /// 当定位发生错误时,会调用代理的此方法。
  100. public func bmkLocationManager(_ manager: BMKLocationManager, didFailWithError error: Error?) {
  101. NXLLog("定位错误");
  102. }
  103. /// 定位权限状态改变时回调函数
  104. public func bmkLocationManager(_ manager: BMKLocationManager, didChange status: CLAuthorizationStatus) {
  105. NXLLog("定位权限改变时候的回调");
  106. initLocation { (locationModel) in
  107. }
  108. }
  109. }
  110. // MARK: - 定位授权
  111. extension BaiduMapManager: BMKLocationAuthDelegate {
  112. public func onCheckPermissionState(_ iError: BMKLocationAuthErrorCode) {
  113. if (0 == iError.rawValue) {
  114. NXLLog("授权成功");
  115. initLocation { (locationModel) in
  116. }
  117. } else {
  118. NXLLog("授权失败 \(iError)");
  119. LocationModel.shared().setLocationModel(locationModel: locationModel!)
  120. }
  121. }
  122. }