如何从存储在CloudKit中的位置反转地理代码的经纬度?(How to reverse geocode latitude and longitude from location stored in CloudKit? - Swift 3)
我想弄清楚如何将存储在
CloudKit
的CLLocation
为地理编码。 我将该位置存储在记录中,我知道它存储为经度和纬度。 这是我的记录。 经纬度显示我现在只是拿出来了。 不过,我希望能够使用户可读的位置,所以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 inCloudKit
. 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 inCloudKit
.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
最满意答案
通过将时间(小时,分钟,秒)设置为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); } } } }
相关问答
更多-
Etherpad和Google Wave使用操作变换 。 这里有一篇关于他们在Google Wave中使用的文章 。 Etherpad and Google Wave use Operational Transforms. Here's an article about their use in Google Wave.
-
不。 Google为不同的服务提供单独的API。 事实上,你可以在这里看到清单。 Nope. Google has separate APIs for different services. In fact, you can see the list here.
-
我没有做任何事来解决这个问题。 它刚开始工作一天。 我认为它是谷歌API的某种错误,他们可以解决这个问题,但我从来没有在其他论坛或帮助网站上发现任何其他错误实例。 I didn't do anything to fix this. It just started working one day. I assume it was some kind of bug with Google's API that they got around to fixing, but I never found any ot ...
-
通过将时间(小时,分钟,秒)设置为0,确保日期匹配以便能够比较它们。 function sendEmails() { var spreadsheet = SpreadsheetApp.openById('[Spreadsheet_Id]'); var sheet = spreadsheet.getSheets()[4]; var startRow = 2; // First row of data to process va ...
-
您可以使用COUNTIF函数和单元格的相对引用来实现此目的。 以下公式将仅填充“ Name列的第一个匹配项 =if(COUNTIF($A$1:$A1,A1)=1,A1,"") 这是一个有效的例子 You can use COUNTIF function with relative referencing of cells to achieve this. The following formula will populate the first occurrence of the Name column ...
-
目前看来,这是不可能的。 我认为这个问题 (您可能希望明星,以便您可以在线程中接收更新)最好地描述它。 第7篇文章摘要: 此功能请求是将相同的脚本功能添加到文档中,就像在电子表格中一样。 即菜单项等 As it stands, this is not possible. I think this issue (which you might like to star so that you can receive updates in the thread) best describes it. Summa ...
-
这是包含您正在寻找的参数的代码。 function generateUserUsageReport() { var today = new Date(); var oneWeekAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000); var timezone = Session.getScriptTimeZone(); var date = Utilities.formatDate(oneWeekAgo, timezone, 'yyyy-MM-d ...
-
如何使用Google App Engine自动化Google文档?(How can I automate google docs with Google App Engine?)[2023-03-25]
目前没有API直接创建谷歌文档,除了:1)制作谷歌应用程序脚本服务,它可以访问docs api。 2)创建一个“.doc”然后上传并转换为gdoc。 1是最好的,但燃气服务有一些限制,如配额。 如果您每天只创建几十个/几百个,那么您可以使用配额。 我已经这样做了类似于你的情况。 There is currently no API to create google docs directly except for: 1) make a google apps script service, which doe ... -
今天,2018-1-11,Google推出了Apps Script Dashboard 。 它列出了所有脚本项目文件,独立和有界。 参考 3个新工具可帮助您改进您的Apps脚本开发和管理体验 Today, 2018-1-11, Google launched the Apps Script Dashboard. It list all the script project files, standalone and bounded. Reference 3 new tools to help improve ...
-
使用两个过滤器: ga:medium==organic;ga:source==google Use two filters: ga:medium==organic;ga:source==google