天气

Scripting 的天气 API 提供对实时天气和天气预报数据的访问,包括当前天气状况、每小时预报和每日预报。用户可以获取指定位置的温度、风速、湿度和降水等天气信息。

类型定义

UnitType

表示带有数值、符号和格式化字符串的测量单位。

1type UnitType = {
2  value: number
3  symbol: string
4  formatted: string
5}

UnitTemperature, UnitSpeed, UnitLength, UnitAngle, UnitPressure

这些类型基于 UnitType,分别表示温度、速度、长度、角度和气压。

WeatherCondition

字符串枚举,描述各种天气状况,包括:

  • clear(晴朗)

  • rain(雨)

  • snow(雪)

  • thunderstorms(雷暴)

  • cloudy(多云)

  • windy(有风)

  • ...

API 方法

Weather.requestCurrent(location: LocationInfo): Promise<CurrentWeather>

获取指定位置的当前天气状况。

参数

  • location: LocationInfo – 需要查询天气的位置。

返回值

返回一个 Promise,解析为 CurrentWeather 对象。

示例

1const location = { latitude: 37.7749, longitude: -122.4194 }
2const weather = await Weather.requestCurrent(location)
3console.log(`当前温度:${weather.temperature.formatted}`)

Weather.requestDailyForecast(location: LocationInfo, options?: { startDate: Date, endDate: Date }): Promise<WeatherDailyForecast>

获取指定位置每日天气预报。你可以选择传入开始日期和结束日期以自定义查询范围。

参数

  • location: LocationInfo – 查询的位置。
  • options.startDate – 预报开始日期。
  • options.endDate – 预报结束日期。

返回值

返回一个 Promise,解析为 WeatherDailyForecast 对象。

示例

1const forecast = await Weather.requestDailyForecast(location, {
2  startDate: new Date(),
3  endDate: new Date(Date.now() + 5 * 24 * 60 * 60 * 1000)
4})
5console.log(`明天天气:${forecast.forecast[1].condition}`)

Weather.requestHourlyForecast(location: LocationInfo, options?: { startDate: Date, endDate: Date }): Promise<WeatherHourlyForecast>

获取指定位置每小时的天气预报。你可以选择传入开始日期和结束日期以自定义查询范围。

参数

  • location: LocationInfo – 查询的位置。
  • options.startDate – 预报开始时间。
  • options.endDate – 预报结束时间。

返回值

返回一个 Promise,解析为 WeatherHourlyForecast 对象。

示例

1const hourlyForecast = await Weather.requestHourlyForecast(location, {
2  startDate: new Date(),
3  endDate: new Date(Date.now() + 3 * 60 * 60 * 1000)
4})
5console.log(`下一小时温度:${hourlyForecast.forecast[0].temperature.formatted}`)

CurrentWeather

表示当前天气状况。

1type CurrentWeather = {
2  temperature: UnitTemperature
3  apparentTemperature: UnitTemperature
4  humidity: number
5  wind: WeatherWind
6  condition: WeatherCondition
7  date: number
8  symbolName: string
9}

WeatherDailyForecast

表示每日天气预报。

1type WeatherDailyForecast = {
2  metadata: WeatherMetadata
3  forecast: DayWeather[]
4}

WeatherHourlyForecast

表示每小时天气预报。

1type WeatherHourlyForecast = {
2  metadata: WeatherMetadata
3  forecast: HourWeather[]
4}

DayWeather

表示每日天气详情。

1type DayWeather = {
2  highTemperature: UnitTemperature
3  lowTemperature: UnitTemperature
4  wind: WeatherWind
5  condition: WeatherCondition
6  date: number
7  symbolName: string
8}

HourWeather

表示每小时天气详情。

1type HourWeather = {
2  temperature: UnitTemperature
3  humidity: number
4  wind: WeatherWind
5  condition: WeatherCondition
6  date: number
7  symbolName: string
8}

使用示例

获取并显示当前天气

1async function displayCurrentWeather() {
2  const location = { latitude: 37.7749, longitude: -122.4194 }
3  const weather = await Weather.requestCurrent(location)
4  console.log(`温度:${weather.temperature.formatted},天气:${weather.condition}`)
5}
6
7displayCurrentWeather()

获取并显示每日天气预报

1async function displayDailyForecast() {
2  const location = { latitude: 37.7749, longitude: -122.4194 }
3  const forecast = await Weather.requestDailyForecast(location, {
4    startDate: new Date(),
5    endDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)
6  })
7  forecast.forecast.forEach(day => {
8    console.log(`日期:${new Date(day.date).toDateString()},天气:${day.condition}`)
9  })
10}
11
12displayDailyForecast()

获取并显示每小时天气预报

1async function displayHourlyForecast() {
2  const location = { latitude: 37.7749, longitude: -122.4194 }
3  const hourlyForecast = await Weather.requestHourlyForecast(location, {
4    startDate: new Date(),
5    endDate: new Date(Date.now() + 5 * 60 * 60 * 1000)
6  })
7  hourlyForecast.forecast.forEach(hour => {
8    console.log(`时间:${new Date(hour.date).toLocaleTimeString()},温度:${hour.temperature.formatted}`)
9  })
10}
11
12displayHourlyForecast()