微信小程序引入和风天气api,展示实时天气

以下步骤为在和风天气官网创建好账号并获取到key后(和风天气官网开发文档):https://dev.qweather.com/docs/start/):

获取天气信息

onShow: function () {
	this.getLocation();//如果需要获取当前位置this.getWeatherInfo(29.950461, 120.309056);//如果只获取一个地方的天气
},
getLocation:function(){
   var that = this;
   wx.getLocation({
     type: 'wgs84',
     success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
       that.getWeatherInfo(latitude, longitude);
    }
  })
},

获取天气方法(天气预报其中也包括了当天的实时天气)

/**
   * 天气
   */
  getWeatherInfo: function (latitude, longitude){
    var _this = this;
    var key = '57d69cf259584bd080b7f6b16b122***';//你自己的key
    //需要在微信公众号的设置-开发设置中配置服务器域名
    // url='https://devapi.qweather.com/v7/weather/now?location=101010100&key=你的KEY' // 当天实况天气
    var url = 'https://free-api.heweather.com/s6/weather?key='+key+'&location=' + longitude + ',' + latitude;
       wx.request({
         url: url, 
         data: {},
         method: 'GET',
         success: function (res) {
           console.log(res);
            var daily_forecast_today = res.data.HeWeather6[0].now;//今天天气
            _this.setData({
              today: daily_forecast_today,
              todyIcon: '../../pic/weather/' + daily_forecast_today.cond_code+'.png', //在和风天气中下载天气的icon图标,根据cond_code_d显示相应的图标
            });
          }
        })
    }

页面展示

<view class="weather-box">
    <image src="{{todyIcon}}" class="weather-icon"></image>
    <view class="weather-info">
        <view class="view1">{{today.cond_txt}}</view>
        <view class="view2">{{today.tmp}}</view>
    </view>
</view>

展示效果

在这里插入图片描述
天气图标在GitHub上获取:https://dev.qweather.com/docs/start/icons/
参考文档: https://zhuanlan.zhihu.com/p/33596283

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值