集成高德 SDK 并替换所有定位相关的 CoreLocation 与 MapKit
This commit is contained in:
49
suixinkan/Features/Location/AMapBootstrap.swift
Normal file
49
suixinkan/Features/Location/AMapBootstrap.swift
Normal file
@ -0,0 +1,49 @@
|
||||
//
|
||||
// AMapBootstrap.swift
|
||||
// suixinkan
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
/// 高德 SDK 隐私合规与 Key 初始化,必须在实例化任何 SDK 类之前完成。
|
||||
enum AMapBootstrap {
|
||||
|
||||
private(set) static var isConfigured = false
|
||||
private static let lock = NSLock()
|
||||
|
||||
/// 在用户已同意 App 隐私协议后配置高德 SDK,幂等。
|
||||
@discardableResult
|
||||
static func configureIfNeeded(appStore: AppStore = .shared) -> Bool {
|
||||
guard appStore.privacyAgreementAccepted else { return false }
|
||||
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
guard !isConfigured else { return true }
|
||||
|
||||
AMapLocationManager.updatePrivacyShow(.didShow, privacyInfo: .didContain)
|
||||
AMapLocationManager.updatePrivacyAgree(.didAgree)
|
||||
MAMapView.updatePrivacyShow(.didShow, privacyInfo: .didContain)
|
||||
MAMapView.updatePrivacyAgree(.didAgree)
|
||||
AMapSearchAPI.updatePrivacyShow(.didShow, privacyInfo: .didContain)
|
||||
AMapSearchAPI.updatePrivacyAgree(.didAgree)
|
||||
|
||||
AMapServices.shared().apiKey = AMapConfig.apiKey
|
||||
AMapServices.shared().enableHTTPS = true
|
||||
isConfigured = true
|
||||
return true
|
||||
}
|
||||
|
||||
/// 要求高德 SDK 已完成合规初始化,否则抛出错误。
|
||||
static func requireConfigured(appStore: AppStore = .shared) throws {
|
||||
guard configureIfNeeded(appStore: appStore) else {
|
||||
throw LocationProviderError.privacyNotAccepted
|
||||
}
|
||||
}
|
||||
|
||||
/// 测试专用:重置初始化状态。
|
||||
static func resetForTesting() {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
isConfigured = false
|
||||
}
|
||||
}
|
||||
64
suixinkan/Features/Location/LocationGeocoder.swift
Normal file
64
suixinkan/Features/Location/LocationGeocoder.swift
Normal file
@ -0,0 +1,64 @@
|
||||
//
|
||||
// LocationGeocoder.swift
|
||||
// suixinkan
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
/// 高德逆地理编码封装,对齐 Android `GeocodeSearch`。
|
||||
final class LocationGeocoder: NSObject, AMapSearchDelegate {
|
||||
|
||||
static let shared = LocationGeocoder()
|
||||
|
||||
private var searchAPI: AMapSearchAPI?
|
||||
private var continuation: CheckedContinuation<String, Never>?
|
||||
|
||||
private override init() {
|
||||
super.init()
|
||||
}
|
||||
|
||||
/// 将坐标解析为可读地址。
|
||||
func reverseGeocode(latitude: Double, longitude: Double) async -> String {
|
||||
do {
|
||||
try AMapBootstrap.requireConfigured()
|
||||
} catch {
|
||||
return ""
|
||||
}
|
||||
|
||||
return await withCheckedContinuation { continuation in
|
||||
self.continuation?.resume(returning: "")
|
||||
self.continuation = continuation
|
||||
|
||||
guard let api = makeSearchAPI() else {
|
||||
continuation.resume(returning: "")
|
||||
return
|
||||
}
|
||||
let request = AMapReGeocodeSearchRequest()
|
||||
request.location = AMapGeoPoint.location(withLatitude: CGFloat(latitude), longitude: CGFloat(longitude))
|
||||
request.requireExtension = true
|
||||
request.radius = 200
|
||||
api.aMapReGoecodeSearch(request)
|
||||
}
|
||||
}
|
||||
|
||||
func onReGeocodeSearchDone(_ request: AMapReGeocodeSearchRequest!, response: AMapReGeocodeSearchResponse!) {
|
||||
let address = response?.regeocode?.formattedAddress ?? ""
|
||||
continuation?.resume(returning: address)
|
||||
continuation = nil
|
||||
}
|
||||
|
||||
func aMapSearchRequest(_ request: Any!, didFailWithError error: Error!) {
|
||||
continuation?.resume(returning: "")
|
||||
continuation = nil
|
||||
}
|
||||
|
||||
private func makeSearchAPI() -> AMapSearchAPI? {
|
||||
if let searchAPI {
|
||||
return searchAPI
|
||||
}
|
||||
guard let api = AMapSearchAPI() else { return nil }
|
||||
api.delegate = self
|
||||
searchAPI = api
|
||||
return api
|
||||
}
|
||||
}
|
||||
100
suixinkan/Features/Location/LocationProvider.swift
Normal file
100
suixinkan/Features/Location/LocationProvider.swift
Normal file
@ -0,0 +1,100 @@
|
||||
//
|
||||
// LocationProvider.swift
|
||||
// suixinkan
|
||||
//
|
||||
|
||||
import CoreLocation
|
||||
import Foundation
|
||||
|
||||
/// 统一定位服务,封装高德 `AMapLocationManager`,对齐 Android `LocationProvider`。
|
||||
final class LocationProvider: NSObject, LocationProviding {
|
||||
|
||||
static let shared = LocationProvider()
|
||||
|
||||
private let permissionManager = CLLocationManager()
|
||||
private let geocoder: LocationGeocoder
|
||||
|
||||
init(geocoder: LocationGeocoder = .shared) {
|
||||
self.geocoder = geocoder
|
||||
super.init()
|
||||
permissionManager.desiredAccuracy = kCLLocationAccuracyBest
|
||||
}
|
||||
|
||||
func requestSnapshot() async throws -> HomeLocationSnapshot {
|
||||
try AMapBootstrap.requireConfigured()
|
||||
try await ensureLocationPermission()
|
||||
|
||||
let manager = makeLocationManager()
|
||||
return try await withCheckedThrowingContinuation { continuation in
|
||||
let started = manager.requestLocation(withReGeocode: true) { location, regeocode, error in
|
||||
if let error {
|
||||
continuation.resume(throwing: error)
|
||||
return
|
||||
}
|
||||
guard let location else {
|
||||
continuation.resume(throwing: LocationProviderError.locationFailed)
|
||||
return
|
||||
}
|
||||
let address = regeocode?.formattedAddress ?? ""
|
||||
continuation.resume(returning: HomeLocationSnapshot(
|
||||
latitude: location.coordinate.latitude,
|
||||
longitude: location.coordinate.longitude,
|
||||
address: address
|
||||
))
|
||||
}
|
||||
if !started {
|
||||
continuation.resume(throwing: LocationProviderError.locationFailed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func requestCoordinate() async throws -> CLLocationCoordinate2D {
|
||||
try AMapBootstrap.requireConfigured()
|
||||
try await ensureLocationPermission()
|
||||
|
||||
let manager = makeLocationManager()
|
||||
return try await withCheckedThrowingContinuation { continuation in
|
||||
let started = manager.requestLocation(withReGeocode: false) { location, _, error in
|
||||
if let error {
|
||||
continuation.resume(throwing: error)
|
||||
return
|
||||
}
|
||||
guard let location else {
|
||||
continuation.resume(throwing: LocationProviderError.locationFailed)
|
||||
return
|
||||
}
|
||||
continuation.resume(returning: location.coordinate)
|
||||
}
|
||||
if !started {
|
||||
continuation.resume(throwing: LocationProviderError.locationFailed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func reverseGeocode(latitude: Double, longitude: Double) async -> String {
|
||||
await geocoder.reverseGeocode(latitude: latitude, longitude: longitude)
|
||||
}
|
||||
|
||||
/// 请求系统定位权限,首次弹窗后等待用户响应。
|
||||
func ensureLocationPermission() async throws {
|
||||
let initialStatus = permissionManager.authorizationStatus
|
||||
if initialStatus == .notDetermined {
|
||||
permissionManager.requestWhenInUseAuthorization()
|
||||
try await Task.sleep(nanoseconds: 500_000_000)
|
||||
}
|
||||
|
||||
let status = permissionManager.authorizationStatus
|
||||
guard status == .authorizedWhenInUse || status == .authorizedAlways else {
|
||||
throw LocationProviderError.permissionDenied
|
||||
}
|
||||
}
|
||||
|
||||
private func makeLocationManager() -> AMapLocationManager {
|
||||
let manager = AMapLocationManager()
|
||||
manager.desiredAccuracy = kCLLocationAccuracyBest
|
||||
manager.locationTimeout = 30
|
||||
manager.reGeocodeTimeout = 10
|
||||
manager.locatingWithReGeocode = true
|
||||
return manager
|
||||
}
|
||||
}
|
||||
54
suixinkan/Features/Location/LocationProviding.swift
Normal file
54
suixinkan/Features/Location/LocationProviding.swift
Normal file
@ -0,0 +1,54 @@
|
||||
//
|
||||
// LocationProviding.swift
|
||||
// suixinkan
|
||||
//
|
||||
|
||||
import CoreLocation
|
||||
import Foundation
|
||||
|
||||
/// 位置采集结果。
|
||||
struct HomeLocationSnapshot: Sendable, Equatable {
|
||||
let latitude: Double
|
||||
let longitude: Double
|
||||
let address: String
|
||||
}
|
||||
|
||||
/// 定位服务协议,便于 ViewModel 注入与单元测试 mock。
|
||||
protocol LocationProviding: Sendable {
|
||||
/// 请求当前坐标与地址。
|
||||
func requestSnapshot() async throws -> HomeLocationSnapshot
|
||||
/// 请求当前坐标。
|
||||
func requestCoordinate() async throws -> CLLocationCoordinate2D
|
||||
/// 逆地理编码。
|
||||
func reverseGeocode(latitude: Double, longitude: Double) async -> String
|
||||
}
|
||||
|
||||
/// 定位错误。
|
||||
enum LocationProviderError: LocalizedError, Equatable {
|
||||
case privacyNotAccepted
|
||||
case permissionDenied
|
||||
case locationFailed
|
||||
|
||||
var errorDescription: String? {
|
||||
switch self {
|
||||
case .privacyNotAccepted:
|
||||
"请先阅读并同意隐私政策"
|
||||
case .permissionDenied:
|
||||
"需要定位权限才能使用此功能"
|
||||
case .locationFailed:
|
||||
"定位失败,请稍后重试"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 订单核销场景定位错误,保持原有提示文案。
|
||||
enum OrderLocationError: LocalizedError {
|
||||
case permissionDenied
|
||||
|
||||
var errorDescription: String? {
|
||||
switch self {
|
||||
case .permissionDenied:
|
||||
"缺少定位权限,功能无法进行"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user