中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
Weather Service Kit获取天气数据
微信扫码分享
导入Weather Service Kit模块及相关公共模块。 import { weatherService } from '@kit.WeatherServiceKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; 在获取天气数据按钮的点击事件中,设置天气数据请求的参数。 let weatherRequest: weatherService.WeatherRequest = { location: { latitude: 0, longitude: 0 }, limitedDatasets: [ weatherService.Dataset.CURRENT, weatherService.Dataset.DAILY, weatherService.Dataset.HOURLY, weatherService.Dataset.MINUTE, weatherService.Dataset.ALERTS, weatherService.Dataset.INDICES, weatherService.Dataset.TIDES ] }; 调用Weather对象的getWeather方法,请求天气数据 try { let weather: weatherService.Weather = await weatherService.getWeather(weatherRequest); hilog.info(0x0000, 'testTag', JSON.stringify(weather)); } catch (err) { let businessError: BusinessError = err as BusinessError; hilog.error(0x0000, 'testTag', JSON.stringify(JSON.stringify(businessError))); }