OOAttanceHeaderView.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // OOAttanceHeaderView.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2018/5/15.
  6. // Copyright © 2018年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. class OOAttanceHeaderView: UIView {
  11. var mapView:BMKMapView!
  12. var userLocation:BMKUserLocation!
  13. var locService: BMKLocationManager!
  14. var searchAddress:BMKGeoCodeSearch!
  15. var annotations:[BMKPointAnnotation] = []
  16. var workPlaces:[OOAttandanceWorkPlace]? {
  17. didSet {
  18. setAnnotations()
  19. }
  20. }
  21. override init(frame: CGRect) {
  22. super.init(frame: frame)
  23. commonInit()
  24. }
  25. required init?(coder aDecoder: NSCoder) {
  26. super.init(coder: aDecoder)
  27. commonInit()
  28. }
  29. func commonInit() {
  30. mapView = BMKMapView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 280))
  31. mapView.showsUserLocation = true
  32. mapView.isSelectedAnnotationViewFront = true
  33. mapView.showMapScaleBar = true
  34. mapView.showMapPoi = true
  35. mapView.showIndoorMapPoi = true
  36. mapView.zoomLevel = 19
  37. //mapView.setCompassImage(UIImage(named: "icon_dingwei2"))
  38. mapView.logoPosition = BMKLogoPositionRightBottom
  39. mapView.userTrackingMode = BMKUserTrackingModeNone
  40. self.backgroundColor = UIColor.white
  41. self.addSubview(mapView)
  42. }
  43. func startBMKMapViewService(){
  44. mapView.delegate = self
  45. locService = BMKLocationManager()
  46. locService.desiredAccuracy = kCLLocationAccuracyBest
  47. //设置返回位置的坐标系类型
  48. locService.coordinateType = .BMK09LL
  49. //设置距离过滤参数
  50. locService.distanceFilter = kCLDistanceFilterNone;
  51. //设置预期精度参数
  52. locService.desiredAccuracy = kCLLocationAccuracyBest;
  53. //设置应用位置类型
  54. locService.activityType = .automotiveNavigation
  55. //设置是否自动停止位置更新
  56. locService.pausesLocationUpdatesAutomatically = false
  57. locService.delegate = self
  58. locService.startUpdatingLocation()
  59. searchAddress = BMKGeoCodeSearch()
  60. searchAddress.delegate = self
  61. }
  62. // MARK:- 计算所有位置是否有一个位置在误差范围内
  63. func calcErrorRange(_ checkinLocation:CLLocationCoordinate2D) -> Bool {
  64. guard let myWorkPlaces = workPlaces else {
  65. return false
  66. }
  67. let currentLocation = checkinLocation
  68. var result = false
  69. for item in myWorkPlaces {
  70. let longitude = Double((item.longitude)!)
  71. let latitude = Double((item.latitude)!)
  72. let eRange = item.errorRange!
  73. let theLocation = CLLocationCoordinate2DMake(latitude!,longitude!)
  74. result = BMKCircleContainsCoordinate(theLocation,currentLocation,Double(eRange))
  75. if result == true {
  76. break
  77. }
  78. }
  79. return result
  80. }
  81. // MARK:- 设置所有位置标注点
  82. func setAnnotations() {
  83. workPlaces?.forEach({ (workPlace) in
  84. self.setAnnotation(workPlace)
  85. })
  86. mapView.addAnnotations(annotations)
  87. }
  88. private func setAnnotation(_ workPlace:OOAttandanceWorkPlace) {
  89. let annotation = BMKPointAnnotation()
  90. let longitude = Double((workPlace.longitude)!)
  91. let latitude = Double((workPlace.latitude)!)
  92. DDLogDebug("placeAlias=\(workPlace.placeAlias ?? ""),longitude=\(String(describing: longitude)),latitude=\(latitude)")
  93. annotation.coordinate = CLLocationCoordinate2DMake(latitude!,longitude!);
  94. annotation.title = workPlace.placeAlias ?? ""
  95. annotation.subtitle = workPlace.placeName ?? ""
  96. annotations.append(annotation)
  97. }
  98. func stopBMKMapViewService() {
  99. locService.stopUpdatingLocation()
  100. locService.delegate = nil
  101. searchAddress.delegate = nil
  102. mapView.delegate = nil
  103. }
  104. }
  105. // MARK: - BMKMapViewDelegate
  106. extension OOAttanceHeaderView:BMKMapViewDelegate {
  107. func mapView(_ mapView: BMKMapView!, viewFor annotation: BMKAnnotation!) -> BMKAnnotationView! {
  108. let reuseIdentifier = "myAnnotaionView"
  109. if annotation.isKind(of: BMKPointAnnotation.self) {
  110. var newAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)
  111. if newAnnotationView == nil {
  112. newAnnotationView = BMKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
  113. }
  114. newAnnotationView?.image = UIImage(named:"icon_dingwei2")
  115. return newAnnotationView
  116. }
  117. return nil
  118. }
  119. func mapViewDidFinishLoading(_ mapView: BMKMapView!) {
  120. DDLogDebug("mapViewDidFinishLoading")
  121. }
  122. func mapViewDidFinishRendering(_ mapView: BMKMapView!) {
  123. DDLogDebug("mapViewDidFinishRendering")
  124. }
  125. }
  126. extension OOAttanceHeaderView: BMKLocationManagerDelegate {
  127. func bmkLocationManager(_ manager: BMKLocationManager, didUpdate location: BMKLocation?, orError error: Error?) {
  128. if let loc = location?.location {
  129. DDLogDebug("当前位置,\(loc.coordinate.latitude),\(loc.coordinate.longitude)")
  130. let user = BMKUserLocation()
  131. user.location = loc
  132. mapView.updateLocationData(user)
  133. mapView.centerCoordinate = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude)
  134. //搜索到指定的地点
  135. let re = BMKReverseGeoCodeSearchOption()
  136. re.location = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude)
  137. let _ = searchAddress.reverseGeoCode(re)
  138. }else {
  139. DDLogError("没有获取到定位信息!!!!!")
  140. }
  141. }
  142. }
  143. extension OOAttanceHeaderView:BMKGeoCodeSearchDelegate {
  144. func onGetReverseGeoCodeResult(_ searcher: BMKGeoCodeSearch?, result: BMKReverseGeoCodeSearchResult?, errorCode error: BMKSearchErrorCode) {
  145. //发送定位的实时位置及名称信息
  146. if let location = result?.location, calcErrorRange(location) == true {
  147. NotificationCenter.post(customeNotification: .location, object: result)
  148. }else{
  149. NotificationCenter.post(customeNotification: .location, object: nil)
  150. }
  151. }
  152. func onGetGeoCodeResult(_ searcher: BMKGeoCodeSearch!, result: BMKGeoCodeSearchResult!, errorCode error: BMKSearchErrorCode) {
  153. if Int(error.rawValue) == 0 {
  154. DDLogDebug("result \(String(describing: result))")
  155. }else{
  156. DDLogDebug("result error errorCode = \(Int(error.rawValue))")
  157. }
  158. }
  159. }