gpt4 book ai didi

swift - 如何从 CloudKit 中存储的位置反转地理编码纬度和经度? - swift 3

转载 作者:行者123 更新时间:2023-11-30 12:43:03 24 4
gpt4 key购买 nike

我正在尝试找出如何对存储在 CloudKit 中的 CLLocation 进行反向地理编码。我将位置存储在记录中,并且我知道它存储为纬度和经度。 Here's my record. The latitude and longitude appear I just took them out for now.不过,我希望能够使位置用户可读,因此又名反向地理编码,以获取城市和州。到目前为止,我已经看过这里,但没有任何关于我可以存储在 CloudKit 中的位置的反向地理编码。

这是我的模型:

class Peek: CloudKitSyncable {

static let kType = "Peek"
static let kPhotoData = "photoData"
static let kTimeStamp = "timestamp"
static let kTitle = "title"
static let kText = "text"
static let kLocation = "location"
static let kCity = "city"

let title: String
let text: String
let photoData: Data?
let timestamp: Date
var location: CLLocation
var comments: [Comment]
var photo: UIImage? {

guard let photoData = self.photoData else { return nil }
return UIImage(data: photoData)
}

init(title: String, timestamp: Date = Date(), text: String, photoData: Data?, comments: [Comment] = [], location: CLLocation) {
self.title = title
self.timestamp = timestamp
self.text = text
self.photoData = photoData
self.comments = comments
self.location = location
}

var recordType: String {
return Peek.kType
}

var cloudKitRecordID: CKRecordID?

convenience required init?(record: CKRecord) {

guard let timestamp = record.creationDate,
let photoAsset = record[Peek.kPhotoData] as? CKAsset,
let title = record[Peek.kTitle] as? String,
let text = record[Peek.kText] as? String,
let location = record[Peek.kLocation] as? CLLocation else { return nil }
let photoData = try? Data(contentsOf: photoAsset.fileURL)
self.init(title: title, timestamp: timestamp, text: text, photoData: photoData, location: location)
cloudKitRecordID = record.recordID
}

fileprivate var temporaryPhotoURL: URL {
let temporaryDirectory = NSTemporaryDirectory()
let temporaryDirectoryURL = URL(fileURLWithPath: temporaryDirectory)
let fileURL = temporaryDirectoryURL.appendingPathComponent(UUID().uuidString).appendingPathExtension("jpg")

try? photoData?.write(to: fileURL, options: .atomic)

return fileURL
}

}
extension CKRecord {

convenience init(_ peek: Peek) {
let recordID = CKRecordID(recordName: UUID().uuidString)
self.init(recordType: peek.recordType, recordID: recordID)

self[Peek.kTitle] = peek.title as String? as CKRecordValue?
self[Peek.kText] = peek.text as String? as CKRecordValue?
self[Peek.kTimeStamp] = peek.timestamp as CKRecordValue?
self[Peek.kLocation] = peek.location as CKRecordValue?
self[Peek.kPhotoData] = CKAsset(fileURL: peek.temporaryPhotoURL)
}
}

我还有一个 LocationManager 文件:

class LocationManager: NSObject {

static let sharedInstance = LocationManager()

override init() {
super.init()
locationManager.delegate = self
}

var locationManager = CLLocationManager()
var currentLocation: CLLocation?

func requestCurrentLocation() {
locationManager.requestLocation()
}
}

extension LocationManager: CLLocationManagerDelegate {

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
currentLocation = locations.first
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("error: \(error.localizedDescription)")
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
locationManager.requestLocation()
}
}
}

最佳答案

Apple 提供了一种内置于 Core Location 的 CLGeocoder 类中的方法。 Here are the docs 。如果成功,完成处理程序将允许您访问一组 CLPlacemark,这样您就可以获取其中一个并访问您需要的任何人类可读元素。变量的名称非常通用,可以覆盖世界各地的位置,因此您必须深入挖掘才能准确找到您需要的内容。 Check the docs有关可用变量的确切详细信息,请访问 CLPlacemark。在您的特定情况下,您将分别需要城市和州的 localityadministrativeArea

用法如下:

let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location) { (placemarks, error) in
guard let placemarks = placemarks, let placemark = placemarks.first else { return }
if let city = placemark.locality, let state = placemark.administrativeArea {
//Set your labels or whatever
}
}

关于swift - 如何从 CloudKit 中存储的位置反转地理编码纬度和经度? - swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42015272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com