ios - 获取日出和日落时间
问题描述
我正在尝试获取日出和日落,我正在使用开放天气 API 来获取纬度和经度。有没有办法使用纬度和经度来获取任何城市的日出和日落时间?
extension WeatherViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
let lat = location.coordinate.latitude
let lon = location.coordinate.longitude
weatherManager.fetchWeather(latitude: lat, longitude: lon)
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
}
我的天气经理
var delegate: weatherManagerDelegate?
func fetchWeather(cityName: String){
let urlString = "\(weatherURL)&q=\(cityName)"
performRequest(with: urlString)
}
func fetchWeather(latitude: CLLocationDegrees, longitude: CLLocationDegrees){
let urlString = "\(weatherURL)&lat=\(latitude)&lon=\(longitude)"
performRequest(with: urlString)
}
解决方案
找出答案:
对于 json,我使用了 SwiftyJson 库.......
let sunriseTime = myResult["sys"]["sunrise"].doubleValue
let sunriseDate = Date(timeIntervalSince1970: sunriseTime)
let formatter = DateFormatter()
formatter.dateStyle = .none
formatter.timeStyle = .medium
let formattedTime = formatter.string(from: sunriseDate)
DispatchQueue.main.async {
self.lblwindspeed.text = "Sunrise: \(formattedTime)"
}
推荐阅读
- c# - 如何检测 c# 表单上的碰撞
- python - Python - 将 pandas 与强化学习结合使用
- javascript - 在 React 中,当它是 useEffect 的依赖项而不触发另一个 API 调用时,如何将值附加到功能状态?
- javascript - 找不到带有 Jest 的模块
- kotlin - Android Studios:类型推断失败。类型参数 T 的值应该在输入类型中提到
- r - 如何将 install_github() 用于托管在 Github 上的 .tar.gz 文件
- python - Python 包导入
- sql - 如何使用查询识别作业正在使用存储过程或 SSIS 包
- c# - 当前上下文中不存在名称“binarySearchRecursive”(c#)
- r - 如何使所有ggplot barplot出现