LocationManager.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // LocationManager.swift
  3. // Pods
  4. //
  5. // Created by Groot on 2025/5/15.
  6. //
  7. import Foundation
  8. import CoreLocation
  9. import Flutter
  10. class LocationManager: NSObject {
  11. static let shared = LocationManager()
  12. private var locationUpdateEventChannel: FlutterEventChannel?
  13. private var locationEventSink: FlutterEventSink?
  14. private let locationManager = CLLocationManager()
  15. var onLocationUpdate: ((CLLocation) -> Void)?
  16. private override init() {
  17. super.init()
  18. locationManager.delegate = self
  19. setup()
  20. }
  21. func start(withMessenger messenger: FlutterBinaryMessenger) -> Bool {
  22. // permission detect
  23. switch CLLocationManager.authorizationStatus() {
  24. case .authorizedWhenInUse, .authorizedAlways:
  25. locationManager.startUpdatingLocation()
  26. setupMessenger(messenger: messenger)
  27. return true
  28. case .notDetermined:
  29. locationManager.requestWhenInUseAuthorization()
  30. return false
  31. default:
  32. print("LocationManager: authorizationStatus: \(CLLocationManager.authorizationStatus())")
  33. return false
  34. }
  35. }
  36. func stop() {
  37. locationManager.stopUpdatingLocation()
  38. }
  39. func setupMessenger(messenger: FlutterBinaryMessenger) {
  40. guard locationUpdateEventChannel == nil && locationEventSink == nil else { return }
  41. locationUpdateEventChannel = FlutterEventChannel(name: MapKitConstans.locationUpdateEventChannelName, binaryMessenger: messenger)
  42. locationUpdateEventChannel?.setStreamHandler(self)
  43. }
  44. func setup() {
  45. // locationManager.allowsBackgroundLocationUpdates = true
  46. locationManager.pausesLocationUpdatesAutomatically = false
  47. locationManager.desiredAccuracy = kCLLocationAccuracyBest
  48. locationManager.distanceFilter = 5.0
  49. locationManager.activityType = .fitness
  50. locationManager.showsBackgroundLocationIndicator = false
  51. }
  52. }
  53. extension LocationManager: CLLocationManagerDelegate {
  54. func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  55. guard let location = locations.last else { return }
  56. onLocationUpdate?(location)
  57. if let eventSink = locationEventSink {
  58. // 转换GPS坐标系到中国坐标系
  59. let transformedCoordinate = location.coordinate.wgc84ToGCJ02
  60. let atLocation = ATMapLocation.fromLocation(location: location)
  61. atLocation.longitude = transformedCoordinate.longitude
  62. atLocation.latitude = transformedCoordinate.latitude
  63. let jsonMessage = atLocation.toJson()
  64. print(jsonMessage)
  65. eventSink(jsonMessage)
  66. }
  67. }
  68. func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
  69. print("LocationManager: didFailWithError: \(error)")
  70. }
  71. }
  72. extension LocationManager: FlutterStreamHandler {
  73. func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
  74. locationEventSink = events
  75. return nil
  76. }
  77. func onCancel(withArguments arguments: Any?) -> FlutterError? {
  78. locationEventSink = nil
  79. return nil
  80. }
  81. }