IMLocationChooseController.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // IMLocationChooseController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2020/6/18.
  6. // Copyright © 2020 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. typealias O2LocationChooseCallback = (_ result: O2LocationData) -> Void ///< 定义确认回调
  11. class IMLocationChooseController: UIViewController {
  12. static func openChooseLocation(callback: @escaping O2LocationChooseCallback) -> IMLocationChooseController {
  13. let vc = IMLocationChooseController()
  14. vc.callback = callback
  15. return vc
  16. }
  17. @IBOutlet weak var mapContainerView: UIView!
  18. @IBOutlet weak var addressLabel: UILabel!
  19. var callback: O2LocationChooseCallback?
  20. private var mapView: BMKMapView!
  21. private var locService: BMKLocationManager!
  22. private var searchAddress: BMKGeoCodeSearch!
  23. private var annotation: BMKPointAnnotation!
  24. private var result: O2LocationData?
  25. override func viewDidLoad() {
  26. super.viewDidLoad()
  27. self.title = "选择位置"
  28. self.navigationItem.backBarButtonItem = UIBarButtonItem(image: UIImage(named: "icon_fanhui"), style: .plain, target: nil, action: nil)
  29. //发送按钮
  30. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: .plain, target: self, action: #selector(sendLocation))
  31. self.showLoading()
  32. self.addressLabel.text = "点击地图选择位置"
  33. //初始化地图和定位
  34. let containerFrame = self.mapContainerView.frame
  35. mapView = BMKMapView(frame: CGRect(x: 0, y: 0, width: containerFrame.width, height: containerFrame.height))
  36. mapView.zoomLevel = 17
  37. mapView.showMapPoi = true
  38. mapView.showIndoorMapPoi = true
  39. self.mapContainerView.addSubview(mapView)
  40. mapView.delegate = self
  41. locService = BMKLocationManager()
  42. locService.delegate = self
  43. locService.startUpdatingLocation()
  44. searchAddress = BMKGeoCodeSearch()
  45. searchAddress.delegate = self
  46. }
  47. @objc private func sendLocation() {
  48. guard let r = self.result else {
  49. self.showError(title: "请选择一个位置!")
  50. return
  51. }
  52. self.callback?(r)
  53. self.navigationController?.popViewController(animated: false)
  54. }
  55. deinit {
  56. mapView.delegate = nil
  57. locService.delegate = nil
  58. locService.stopUpdatingLocation()
  59. searchAddress.delegate = nil
  60. }
  61. }
  62. extension IMLocationChooseController: BMKMapViewDelegate, BMKLocationManagerDelegate, BMKGeoCodeSearchDelegate {
  63. //定位
  64. func bmkLocationManager(_ manager: BMKLocationManager, didUpdate location: BMKLocation?, orError error: Error?) {
  65. if let loc = location?.location {
  66. DDLogDebug("定位到 当前位置,\(loc.coordinate.latitude),\(loc.coordinate.longitude)")
  67. let user = BMKUserLocation()
  68. user.location = loc
  69. mapView.updateLocationData(user)
  70. mapView.centerCoordinate = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude)
  71. //定位完成停止定位
  72. locService.stopUpdatingLocation()
  73. self.hideLoading()
  74. }
  75. }
  76. //位置搜索
  77. func onGetReverseGeoCodeResult(_ searcher: BMKGeoCodeSearch!, result: BMKReverseGeoCodeSearchResult!, errorCode error: BMKSearchErrorCode) {
  78. DDLogDebug("获取到地址: \(String(describing: result.address))")
  79. self.addressLabel.text = result.address
  80. //todo 地址和位置经纬度要保存起来
  81. if self.result == nil {
  82. self.result = O2LocationData()
  83. }
  84. self.result?.address = result.address
  85. self.result?.addressDetail = result.sematicDescription
  86. self.result?.latitude = result.location.latitude
  87. self.result?.longitude = result.location.longitude
  88. }
  89. //地图
  90. func mapView(_ mapView: BMKMapView!, onClickedMapPoi mapPoi: BMKMapPoi!) {
  91. if annotation == nil {
  92. annotation = BMKPointAnnotation()
  93. annotation.coordinate = mapPoi.pt
  94. mapView.addAnnotation(annotation)
  95. } else {
  96. annotation.coordinate = mapPoi.pt
  97. }
  98. //反向查询具体地址名称
  99. let re = BMKReverseGeoCodeSearchOption()
  100. re.location = mapPoi.pt
  101. let flag = searchAddress.reverseGeoCode(re)
  102. DDLogDebug("根据位置坐标,查询地址信息 \(flag)")
  103. }
  104. func mapView(_ mapView: BMKMapView!, onClickedMapBlank coordinate: CLLocationCoordinate2D) {
  105. //单击
  106. if annotation == nil {
  107. annotation = BMKPointAnnotation()
  108. annotation.coordinate = coordinate
  109. mapView.addAnnotation(annotation)
  110. } else {
  111. annotation.coordinate = coordinate
  112. }
  113. //反向查询具体地址名称
  114. let re = BMKReverseGeoCodeSearchOption()
  115. re.location = coordinate
  116. let flag = searchAddress.reverseGeoCode(re)
  117. DDLogDebug("根据位置坐标,查询地址信息 \(flag)")
  118. }
  119. }