我们在HarmonyOS开发中,如何一个简单的应用内实时天气查询功能?

阅读 582
1 个回答

需要在应用中查询并展示实时天气信息。

使用天气服务API来实现。

参见:https://developer.huawei.com/consumer/cn/doc/

import weatherService from '@ohos.weatherService';

@Entry
@Component
struct WeatherQuery {
  getWeather(city: string) {
    weatherService.getRealtimeWeather(city, (err, weatherData) => {
      if (!err) {
        console.log(`当前${city}天气:${weatherData}`);
      }
    });
  }

  build() {
    Column() {
      Button('查询天气')
        .onClick(() => this.getWeather('北京'));
    }
  }
}

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。