如何从存储在CloudKit中的位置反转地理编码的纬度和经度? - 斯威夫特3

时间:2017-02-03 01:23:43

标签: swift xcode core-location cllocation reverse-geocoding

我正在尝试找出如何反转CLLocation中存储的CloudKit的地理编码。我将位置存储在记录中,我知道它存储为纬度和经度。 Here's my record. The latitude and longitude appear I just took them out for now. 但是,我希望能够使位置用户可读,因此AKA反向地理编码,以获得城市和州。到目前为止,我已经在这里看了一下,但没有对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()
        }
    }
}

1 个答案:

答案 0 :(得分:0)

Apple提供了一个内置于Core Location的CLGeocoder类的方法。 Here are the docs。如果成功,完成处理程序将允许您访问CLPlacemark数组,因此您可以获取其中一个并访问您需要的任何人类可读元素。变量的名称非常通用,涵盖了世界各地的位置,因此您需要深入挖掘才能找到您需要的内容。有关您可用变量的详细信息,请CLPlacemark locality在您的特定情况下,您分别需要城市和州的administrativeArealet 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 } }

用法是这样的:

   /**
     * Get a subscription instance by name.
     *
     * @param  string  $subscription
     * @return \Laravel\Cashier\Subscription|null
     */
    public function subscription($subscription = 'default')
    {
        return $this->subscriptions->sortByDesc(function ($value) {
            return $value->created_at->getTimestamp();
        })
        ->first(function ($value) use ($subscription) {
            return $value->name === $subscription;
        });
    }