jsdw_ios/QuickLocation/Section/PigeonMessage/PigeonWatermarkCameraVC.swift

85 lines
2.5 KiB
Swift

//
// PigeonWatermarkCameraVC.swift
// QuickLocation
//
import UIKit
import CoreLocation
import SwiftyUserDefaults
#if !targetEnvironment(simulator)
import AMapSearchKit
#endif
final class PigeonWatermarkCameraVC: BaseViewController {
var onPhoto: ((UIImage) -> Void)?
private var rootView: PigeonWatermarkCameraView!
#if !targetEnvironment(simulator)
private let searchAPI = AMapSearchAPI()
#endif
override func loadView() {
rootView = PigeonWatermarkCameraView(frame: UIScreen.main.bounds)
view = rootView
}
override func viewDidLoad() {
super.viewDidLoad()
#if !targetEnvironment(simulator)
searchAPI?.delegate = self
#endif
rootView.onClose = { [weak self] in
self?.navigationController?.popViewController(animated: true)
}
rootView.onConfirm = { [weak self] image in
guard let self else { return }
self.onPhoto?(image)
self.navigationController?.popViewController(animated: true)
}
requestAddress()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
rootView.startSession()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
rootView.stopSession()
}
private func requestAddress() {
guard let lat = Defaults[\.currentLatitude],
let lon = Defaults[\.currentLongitude] else { return }
let coord = CLLocationCoordinate2D(latitude: lat, longitude: lon)
guard CLLocationCoordinate2DIsValid(coord) else { return }
#if !targetEnvironment(simulator)
let regeo = AMapReGeocodeSearchRequest()
regeo.location = AMapGeoPoint.location(
withLatitude: CGFloat(coord.latitude),
longitude: CGFloat(coord.longitude)
)
regeo.requireExtension = true
searchAPI?.aMapReGoecodeSearch(regeo)
#endif
}
}
#if !targetEnvironment(simulator)
extension PigeonWatermarkCameraVC: AMapSearchDelegate {
func onReGeocodeSearchDone(_ request: AMapReGeocodeSearchRequest!, response: AMapReGeocodeSearchResponse!) {
let address = response.regeocode?.formattedAddress
?? response.regeocode?.pois?.first?.name
?? ""
rootView.updateAddress(address)
}
func aMapSearchRequest(_ request: Any!, didFailWithError error: Error!) {
print("Pigeon watermark regeo error: \(error.localizedDescription)")
}
}
#endif