微信小程序实现一个天气预报应用程序

第一步创建一个项目

第二步项目目录下找到 pages/index/index.wxml 文件

<view class="container">
  <view class="header">
    <input class="input" placeholder="请输入城市名称" bindinput="onInput" value="{
   { inputValue }}" />
    <button class="button" bindtap="searchWeather">查询</button>
  </view>
  <view class="weather-info" wx:if="{
   { weatherData }}">
    <view class="city">{
   {
    weatherData.city }}</view>
    <view class="temperature">温度:{
   {
    weatherData.temperature }}</view>
    <view class="weather">{
   {
    weatherData.weather }}</view>
    <view class="wind">风向:{
   {
    weatherData.wind }}</view>
    <view class="humidity">湿度:{
   {
    weatherData.humidity }}</view>
  </view>
</view>

第三步在 pages/index/index.wxss 文件中写入样式

.container {
   
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
}

.header {
   
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.input {
   
  flex-grow: 1;
  padding: 10px;
  border: 1px solid #CCC;
  border-radius: 4px;
}

.button {
   
  padding: 10px 20px;
  margin-left: 10px;
  background-color: #007AFF;
  color: #FFF;
  border-radius: 4px;
}

.weather-info {
   
  display: flex;
  flex-direction: column;
  align-items: center;
}

.city {
   
  font-size: 24px;
  margin-bottom: 10px;
}

.temperature {
   
  font-size: 16px;
  margin-bottom: 10px;
}

.weather {
   
  font-size: 16px;
  margin-bottom: 10px;
}

.wind {
   
  font-size: 16px;
  margin-bottom: 10px;
}

.humidity {
   
  font-size: 16px;
  margin-bottom: 10px;
}

第四步在 pages/index/index.js 文件中添加以下代码

Page({
   
  data: {
   
    inputValue: '', // 输入框的值
    weatherData: null // 天气数据
  },
  onInput(e) {
   
    this.setData({
   
      inputValue: e.detail.value
    });
  },
  searchWeather() {
   
    const that = this;
    wx.request({
   
      url: 'https://api.weatherapi.com/v1/current.json',
      data: {
   
        key: 'YOUR_API_KEY',
        q: this.data.inputValue,
        aqi: 'no'
      },
      success(res) {
   
        const weather = res.data.current;
        that.setData({
   
          weatherData: {
   
            city: res.data.location.name,
            temperature: weather.temp_c,
            weather: weather.condition.text,
            wind: `${
     weather.wind_dir} ${
     weather.wind_kph}km/h`,
            humidity: `${
     weather.humidity}%`
          }
        });
      }
    });
  }
});

项目简介

这个示例展示了一个天气预报应用程序,包括一个输入框和一个查询按钮,用于查询指定城市的实时天气信息。在下方显示了城市名称、温度、天气状况、风向和湿度。

相关推荐

  1. 程序实现一个天气预报应用程序

    2023-12-27 07:18:02       42 阅读
  2. 程序生成一个天气查询的程序

    2023-12-27 07:18:02       42 阅读
  3. 程序实现一个电影信息查询的应用程序

    2023-12-27 07:18:02       37 阅读
  4. 程序实现一个简单的登录功能

    2023-12-27 07:18:02       38 阅读
  5. 程序实现一个音乐播放器的功能

    2023-12-27 07:18:02       37 阅读
  6. 程序一个录音机

    2023-12-27 07:18:02       4 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-27 07:18:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-27 07:18:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-27 07:18:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-27 07:18:02       18 阅读

热门阅读

  1. C语言:冒泡排序算法的原理

    2023-12-27 07:18:02       27 阅读
  2. 【VS】如何把wpf项目打包成exe文件

    2023-12-27 07:18:02       28 阅读
  3. 数据库的连接池详解

    2023-12-27 07:18:02       32 阅读
  4. 单元测试实战

    2023-12-27 07:18:02       45 阅读
  5. WPF RelativeSource

    2023-12-27 07:18:02       39 阅读
  6. 10分钟了解nextTick,并实现简易版本的nextTick

    2023-12-27 07:18:02       33 阅读
  7. 【Python】FastAPI学习记录(二)

    2023-12-27 07:18:02       44 阅读
  8. 14.bash shell中的for/while/until循环

    2023-12-27 07:18:02       43 阅读
  9. zookeeper 面试

    2023-12-27 07:18:02       31 阅读
  10. node express简单微服务

    2023-12-27 07:18:02       35 阅读
  11. Nginx Unit 1.27.0 发布

    2023-12-27 07:18:02       41 阅读