| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- //
- // LocationManager.swift
- // Pods
- //
- // Created by Groot on 2025/5/15.
- //
- import Foundation
- import CoreLocation
- import Flutter
- class LocationManager: NSObject {
- static let shared = LocationManager()
- private var locationUpdateEventChannel: FlutterEventChannel?
- private var locationEventSink: FlutterEventSink?
- private let locationManager = CLLocationManager()
- var onLocationUpdate: ((CLLocation) -> Void)?
- private override init() {
- super.init()
- locationManager.delegate = self
- setup()
- }
- func start(withMessenger messenger: FlutterBinaryMessenger) -> Bool {
- // permission detect
- switch CLLocationManager.authorizationStatus() {
- case .authorizedWhenInUse, .authorizedAlways:
- locationManager.startUpdatingLocation()
- setupMessenger(messenger: messenger)
- return true
- case .notDetermined:
- locationManager.requestWhenInUseAuthorization()
- return false
- default:
- print("LocationManager: authorizationStatus: \(CLLocationManager.authorizationStatus())")
- return false
- }
- }
- func stop() {
- locationManager.stopUpdatingLocation()
- }
- func setupMessenger(messenger: FlutterBinaryMessenger) {
- guard locationUpdateEventChannel == nil && locationEventSink == nil else { return }
- locationUpdateEventChannel = FlutterEventChannel(name: MapKitConstans.locationUpdateEventChannelName, binaryMessenger: messenger)
- locationUpdateEventChannel?.setStreamHandler(self)
- }
- func setup() {
- // locationManager.allowsBackgroundLocationUpdates = true
- locationManager.pausesLocationUpdatesAutomatically = false
- locationManager.desiredAccuracy = kCLLocationAccuracyBest
- locationManager.distanceFilter = 5.0
- locationManager.activityType = .fitness
- locationManager.showsBackgroundLocationIndicator = false
- }
- }
- extension LocationManager: CLLocationManagerDelegate {
- func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
- guard let location = locations.last else { return }
- onLocationUpdate?(location)
- if let eventSink = locationEventSink {
- // 转换GPS坐标系到中国坐标系
- let transformedCoordinate = location.coordinate.wgc84ToGCJ02
- let atLocation = ATMapLocation.fromLocation(location: location)
- atLocation.longitude = transformedCoordinate.longitude
- atLocation.latitude = transformedCoordinate.latitude
-
- let jsonMessage = atLocation.toJson()
- print(jsonMessage)
- eventSink(jsonMessage)
- }
- }
- func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
- print("LocationManager: didFailWithError: \(error)")
- }
- }
- extension LocationManager: FlutterStreamHandler {
- func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
- locationEventSink = events
- return nil
- }
- func onCancel(withArguments arguments: Any?) -> FlutterError? {
- locationEventSink = nil
- return nil
- }
- }
|