import Foundation import AMapLocationKit extension Notification.Name { static let locationstartEvent = Notification.Name("locationstartEvent") //启动定位 static let updatelocationEvent = Notification.Name("updatelocationEvent") //更新位置事件 } class LocationService: NSObject { static let shared = LocationService() private let TAG = "LocationService" private var locationManager: AMapLocationManager! private var currentLocation: CLLocation? private var currentReGeocode: AMapLocationReGeocode? private var initialized = false private override init() { super.init() } // 初始化定位服务 func initialize() { if initialized { return } print("\(TAG)--------- 初始化高德定位服务") locationManager = AMapLocationManager() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest // 定位精度 locationManager.distanceFilter = 10.0 // 移动距离阈值(单位:米) locationManager.locatingWithReGeocode = true // 是否返回逆地理编码信息 locationManager.allowsBackgroundLocationUpdates = true // 是否允许后台定位 locationManager.locationTimeout = 10 // 定位超时时间(秒) locationManager.reGeocodeTimeout = 5 // 逆地理编码超时时间(秒) locationManager.startUpdatingLocation() initialized = true } // 获取缓存的位置信息 func getCachedLocation() -> [String: String]? { guard let location = currentLocation else { return nil } var locationInfo: [String: String] = [ "user_lat": "\(location.coordinate.latitude)", "user_lng": "\(location.coordinate.longitude)" ] if let regeocode = currentReGeocode { let address = [ regeocode.province, // 省 regeocode.city, // 市 regeocode.district, // 区 regeocode.street, // 街道 regeocode.number // 门牌号 ].compactMap { $0 }.joined(separator: "") locationInfo["user_location"] = address } return locationInfo } // 获取当前位置(带回调) func getCurrentLocation(completion: @escaping ([String: String]?, Error?) -> Void) { if let cached = getCachedLocation() { completion(cached, nil) } else { let error = NSError( domain: "LocationService", code: -1, userInfo: [NSLocalizedDescriptionKey: "当前位置还未获取"] ) completion(nil, error) } } // 停止定位服务 func stop() { locationManager.stopUpdatingLocation() locationManager = nil currentLocation = nil currentReGeocode = nil initialized = false } } // MARK: - AMapLocationManagerDelegate extension LocationService: AMapLocationManagerDelegate { // 定位更新回调 func amapLocationManager(_ manager: AMapLocationManager!, didUpdate location: CLLocation!, reGeocode: AMapLocationReGeocode!) { currentLocation = location currentReGeocode = reGeocode print("\(TAG)--------- 定位更新: \(location.coordinate.latitude), \(location.coordinate.longitude)") var locationInfo: [String: String] = [ "user_lat": "\(location.coordinate.latitude)", "user_lng": "\(location.coordinate.longitude)" ] if let regeocode = currentReGeocode { let address = [ reGeocode.province, // 省 reGeocode.city, // 市 reGeocode.district, // 区 reGeocode.street, // 街道 reGeocode.number // 门牌号 ].compactMap { $0 }.joined(separator: "") print("\(TAG)--------- 定位地址: \(address)") locationInfo["user_location"] = address } NotificationCenter.default.post( name: .updatelocationEvent, object: nil, userInfo: locationInfo ) } // 定位失败回调 func amapLocationManager(_ manager: AMapLocationManager!, didFailWithError error: Error!) { print("\(TAG)--------- 定位失败: \(error.localizedDescription)") } // 权限变更回调 func amapLocationManager(_ manager: AMapLocationManager!, doRequireLocationAuth locationManager: CLLocationManager!) { locationManager.requestWhenInUseAuthorization() } }