【发布时间】:2020-10-23 12:07:08
【问题描述】:
我目前能够确定用户的纬度/经度并访问坐标。我将如何对坐标进行反向地理编码以确定用户当前所在的城市以便我可以显示它?
这是我当前的 LocationManager 文件
import Foundation
import MapKit
class LocationManager: NSObject, ObservableObject {
private let locationManager = CLLocationManager()
@Published var location: CLLocation? = nil
override init() {
super.init()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.distanceFilter = kCLDistanceFilterNone
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
}
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
}
// MARK: - CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
print("notDetermined")
manager.requestWhenInUseAuthorization()
default:
break
}
}
}
extension LocationManager: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else {
return
}
self.location = location
}
}
我想将城市信息传递给我的一个视图,并以文本元素的形式输出城市。即“探索奥兰多”
【问题讨论】:
-
这能回答你的问题吗? Convert coordinates to City name?
标签: swiftui core-location