SwiftySuncalc
根据原始的 suncalc 项目 README,
SunCalc is a tiny BSD-licensed JavaScript library for calculating sun position, sunlight phases
(times for sunrise, sunset, dusk, etc.), moon position and lunar phase for the given location
and time, created by Vladimir Agafonkin (@mourner) as a part of the SunCalc.net project.
Most calculations are based on the formulas given in the excellent Astronomy Answers articles
about position of the sun and the planets. You can read about different twilight phases calculated
by SunCalc in the Twilight article on Wikipedia.
该维基百科文章可以在这里找到 here。这个 Swift 库是原始的 suncalc.js 库 的移植,由 Vladimir Agafonkin (@mourner) 编写。本 README
页面上的所有信息均取自原始 suncalc.js 库的 README。
使用方法
// Declare a new SwiftySuncalc object
var suncalc: SwiftySuncalc! = SwiftySuncalc()
// Get moon illumination times for today
var moonIllumination = suncalc.getMoonIllumination(date: Date())
// Get the angle of the moon from the dictionary, `moonIllumination`
var moonAngle = moonIllumination["angle"]
// Find out the times for today (e.g. sunset or sunrise)
var times = suncalc.getTimes(date: Date(), lat: 51.5, lng: -0.5);
// Find out the time for nadir today
var nadir: Double = times["nadir"]
安装
目前,SwiftySuncalc 只通过 CocoaPods 提供用于 Swift 4.x 的支持。将以下内容添加到您的 Podfile
target 'YourTargetApp' do
pod 'SwiftySuncalc', '~> 1.0'
end
然后在您的终端中运行 pod install
,或者在 CocoaPods.app 中运行。或者,为了测试运行,运行以下命令
pod try SwiftySuncalc
参考
日出时间
func getTimes(date: Date, lat: Double, lng: Double) -> Dictionary<String, Date>
此方法调用返回一个具有以下属性的对象,返回一个Date()对象
属性 | 描述 |
---|---|
sunrise |
日出(太阳顶部边缘出现在地平线上) |
sunriseEnd |
日出结束(太阳底部边缘接触到地平线) |
goldenHourEnd |
早黄金时段结束 |
solarNoon |
中午太阳高度(太阳处于最高位置) |
goldenHour |
晚黄金时段开始 |
sunsetStart |
日落开始(太阳底部边缘接触到地平线) |
sunset |
日落(太阳消失在地平线下,开始傍晚民用曙暮光) |
dusk |
黄昏(开始傍晚航海曙暮光) |
nauticalDusk |
航海黄昏(开始傍晚天文曙暮光) |
night |
夜晚开始(足够黑暗,可以进行天文观测) |
nadir |
半夜(夜晚最黑暗的时刻,太阳处于最低位置) |
nightEnd |
夜晚结束(开始清晨天文曙暮光) |
nauticalDawn |
航海黎明(开始清晨航海曙暮光) |
dawn |
黎明(清晨航海曙暮光结束,开始清晨民用曙暮光) |
func addTime(angle: Double, riseName: String, setName: String)
添加自定义时间,包含度数的角度,升起名称和落下的名称。
太阳位置
func getPosition(date: Date, lat: Double, lng: Double) -> Dictionary<String, Double>
返回一个具有以下键的字典
altitude
:月亮的仰角,单位为弧度azimuth
:月亮方位角,单位为弧度distance
:到月亮的距离,单位为千米parallacticAngle
:月亮的视方位角,单位为弧度
月亮亮度
func getMoonIllumination(date: Date) -> Dictionary<String, Double>
返回一个具有以下键的字典
fraction
:月亮被照亮的比例,范围从0.0
(新月)到1.0
(满月)phase
:月亮相位;范围从0.0
到1.0
,如下所述angle
:从圆盘的北点往东计算,月亮被照亮部分的中间角度,单位为弧度;如果角度为负,则月亮盈,如果为正,则月亮减
月亮相位的值应如下解释
相位 | 名称 |
---|---|
0 | 新月 |
亏凸月 | |
0.25 | 上弦月 |
盈凸月 | |
0.5 | 满月 |
亏凸月 | |
0.75 | 下弦月 |
亏凸月 |
从parallacticAngle
中减去angle
,可以得出月亮明亮边缘的顶角(逆时针)。顶角可以用来从观察者的角度绘制月亮形状(例如,月亮躺在它的背上)。
月亮升起和落下的时间
func getMoonTimes(date: Date, lat: Double, lng: Double) -> Dictionary<String, Date?>
提供一个具有以下属性的对象
rise
:月亮升起时间,作为Date
类型set
:月亮落下时间,作为Date
类型alwaysUp
:如果月亮始终不升起或落下,且一直保持在白天 以上 的地平线上,则此属性为true
alwaysDown
:如果月亮始终 低于 地平线,则此属性为true
默认情况下,它将在本地用户的白天(从0到24小时)内搜索月亮升起和落下的时间。