首页 \ 问答 \ 如何从存储在CloudKit中的位置反转地理代码的经纬度?(How to reverse geocode latitude and longitude from location stored in CloudKit? - Swift 3)

如何从存储在CloudKit中的位置反转地理代码的经纬度?(How to reverse geocode latitude and longitude from location stored in CloudKit? - Swift 3)

我想弄清楚如何将存储在CloudKitCLLocation为地理编码。 我将该位置存储在记录中,我知道它存储为经度和纬度。 这是我的记录。 经纬度显示我现在只是拿出来了。 不过,我希望能够使用户可读的位置,所以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()
        }
    }
}

I am trying to figure out how to reverse geocode a CLLocation stored in CloudKit. I have the location stored in a record and I know it stores as latitude and longitude. Here's my record. The latitude and longitude appear I just took them out for now. However I want to be able to make the location user-readable, so AKA reverse geocode it, to get the city and state. And I've looked on here so far but nothing on reverse geocoding the location I can store in CloudKit.

Here is my model:

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

I also have a LocationManager file as well:

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()
        }
    }
}

原文:https://stackoverflow.com/questions/42015272
更新时间:2024-03-14 12:03

最满意答案

通过将时间(小时,分钟,秒)设置为0,确保日期匹配以便能够比较它们。

function sendEmails() {

     var spreadsheet =     SpreadsheetApp.openById('[Spreadsheet_Id]');           

      var sheet = spreadsheet.getSheets()[4];

      var startRow = 2;  // First row of data to process
      var numRows = 10;   // Number of rows to process
  // Fetch the range of cells
  var dataRange = sheet.getRange(startRow, 1, numRows, 4);

  // Fetch values for each row in the Range.
  var data = dataRange.getValues();   

  var emailAddress = 'email@email.com';
  var subject = "Report Today";

  var today = new Date();
  today.setHours(0,0,0,0);

  for (i in data) {
    var row = data[i];
    row[3].setHours(0,0,0,0);
    if( row[3] == today ) { 
        Logger.log("Send email with: " + row);
        var message = row[0]+row[1];
        try {
          MailApp.sendEmail(emailAddress, subject, message);
        } catch(errorDetails) {
          MailApp.sendEmail(emailAddress, "sendEmail script error", errorDetails.message);
        }
      }
  }
}

Make sure that the dates match to be able to compare them, by setting the time (hours, mins, secs) equal to 0.

function sendEmails() {

     var spreadsheet =     SpreadsheetApp.openById('[Spreadsheet_Id]');           

      var sheet = spreadsheet.getSheets()[4];

      var startRow = 2;  // First row of data to process
      var numRows = 10;   // Number of rows to process
  // Fetch the range of cells
  var dataRange = sheet.getRange(startRow, 1, numRows, 4);

  // Fetch values for each row in the Range.
  var data = dataRange.getValues();   

  var emailAddress = 'email@email.com';
  var subject = "Report Today";

  var today = new Date();
  today.setHours(0,0,0,0);

  for (i in data) {
    var row = data[i];
    row[3].setHours(0,0,0,0);
    if( row[3] == today ) { 
        Logger.log("Send email with: " + row);
        var message = row[0]+row[1];
        try {
          MailApp.sendEmail(emailAddress, subject, message);
        } catch(errorDetails) {
          MailApp.sendEmail(emailAddress, "sendEmail script error", errorDetails.message);
        }
      }
  }
}

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。