Files
suixinkan_uikit/suixinkan/Features/Location/LocationGeocoder.swift

65 lines
1.9 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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
}
}