实训第七天:天气预报小程序实现自动定位和布局完善

本文介绍了如何在微信小程序中实现自动定位功能,通过腾讯地图API获取经纬度并解析出地址,结合天气API展示实时天气。同时,展示了两种天气预报页面布局方案:滚动布局和表格布局,并提供了相关代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现自动定位:接入腾讯地图的api,并申请一个key
获取位置接口和经纬度,用位置服务逆解析出ip地址,再将ip地址传值到获取实况天气的接口
代码:
 // wx获取位置接口
    wx.getLocation({
     success: function (res) {
     // 获取到经纬度
    console.log(res);
      // 逆地址解析
          qqmapsdk.reverseGeocoder({
          location:{
            latitude: res.latitude,
            longitude: res.longitude
          },
          success:function(res){
            console.log(res.result.address_component.district.substr(0,2));
            _this.weathertoday(res.result.address_component.district.substr(0, 2));
            _this.weatherweekday(res.result.address_component.district.substr(0, 2));
            }
        })
      }
    });
  },

// 天气api实况天气
  weatherweekday: function (city) {
    var _this = this;
    wx.request({
      url: 'https://www.tianqiapi.com/api/?version=v1',
      data: {
        'city': city
      },
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        _this.setData({
          weatherweek: res.data
        });
        console.log("7日天气 =>",_this.data.weatherweek)
      }
    });
  }
})

2.天气预报页面布局:可以用wx:for循环实现
①滚动布局:

<view class="weekday">7日天气</view>
<scroll-view class="scroll-view_H" scroll-x="true">
   <view class="scroll-view-item_H" wx:for="{{weatherweek.data}}">
      <view>{{item.date}}</view>
   <view>{{item.tem2}}~{{item.tem1}}</view>
      <view class='wea'>{{item.wea}}<image class='wea_img' src='../../weaicon/{{item.wea_img}}.png'></image></view>
      <view>{{item.win[0]}}{{item.win_speed}}</view> 
  </view>
</scroll-view>

②表格布局:

<view class="detail">
  <view class="item">
    <text>温度(℃)</text>
    <text>{{weather.tem}}</text>
  </view>
  <view class="item">
      <text>湿度(%)</text>
    <text>{{humidity}}</text>
  </view> 
  <view class="item">
        <text>PM2.5</text>
    <text>{{weather.air_pm25}}</text>
  </view>
    <view class="item">
      <text>气压(hPa)</text>
      <text>{{weather.pressure}}</text>
    </view>   
  <view class="item">
    <text>风向</text>
    <text>{{weather.win}}</text>
  </view> 
  <view class="item">
   <text>风速</text>
    <text>{{weather.win_meter}}</text></view>  
      <view class="item">
      <text>风速等级</text>
    <text>{{weather.win_speed}}</text>
  </view>  
  <view class="item">
  <text>能见度</text>
    <text>{{weather.visibility}}</text>
  </view>  
</view>

③天气指数布局:
<view class='mar-left'>
<text class='foot'>生活指数</text>
<view>
<image class='photo2' src='../../weaicon/衣服.png'></image>
<view class='tips'>
<text class='word'>穿衣指数 热</text>
<view class='life'>{{weather.air_tips}}</view>
</view>
</view>
<view>
<image class='photo2' src='../../weaicon/药丸.png'></image>
<view class='tips'>
<text class='word'>感冒指数 少发</text>
<view class='life'>{{weather.air_tips}}</view>
</view>
</view>
<view>

实现页面:

 


 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值