Page({
/
* 页面的初始数据
*/
data: {
search_city: ,
imgsrc:100
},
/
* 根据城市获取天气预报
*/
getWeather(city) {
let that = this
//获取实况天气
wx.request({
url: https://free-api.heweather-/s6/weather/now?key=你后台的key&location= + city,
success: function(res) {
if (res.data.HeWeather6[0].status == unknown location) {
wx.showToast({
title: 抱歉!没有该城市的天气预报,
icon: none,
duration: 2000
})
return;
}
console.log(res)
that.setData({
city: city,
tmp: res.data.HeWeather6[0].now.tmp,
imgsrc: res.data.HeWeather6[0].now.cond_code,
wind_dir: res.data.HeWeather6[0].now.wind_dir,
wind_sc: res.data.HeWeather6[0].now.wind_sc,
hum: res.data.HeWeather6[0].now.hum,
pres: res.data.HeWeather6[0].now.pres
})
//获取24小时天气预报
wx.request({
url: https://free-api.heweather-/s6/weather/hourly?key=你后台的key&location= + city,
success: function(res) {
var arr = res.data.HeWeather6[0].hourly
var hourly = []
for (var i = 0; i < arr.length; i++) {
hourly[i] = {
"imgsrc": arr[i].cond_code,
"tmp": arr[i].tmp,
"time": arr[i].time.substring(11),
"wind_dir": arr[i].wind_dir,
"wind_sc": arr[i].wind_sc
}
}
that.setData({
hourly: hourly
})
var weekArray = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
//获取未来7天天气预报
wx.request({
url: https://free-api.heweather-/s6/weather/forecast?key=你后台的key&location= + city,
success: function(result) {
//console.log(result)
var arr = result.data.HeWeather6[0].daily_forecast
var daily_forecast = []
for (var i = 0; i < arr.length; i++) {
daily_forecast[i] = {
d_txt: i == 0 ? "今天" : weekArray[new Date(arr[i].date).getDay()],
d_date: arr[i].date.substring(5),
imgsrc_d: arr[i].cond_code_d,
imgsrc_n: arr[i].cond_code_n,
wind_dir: arr[i].wind_dir,
wind_sc: arr[i].wind_sc,
tmp_max: arr[i].tmp_max,
tmp_min: arr[i].tmp_min,
cond_txt_d: arr[i].cond_txt_d
}
}
that.setData({
daily_forecast: daily_forecast
})
}
})
}
})
}
})
},
bindKeyInput(e) {
this.setData({
search_city: e.detail.value
})
},
search() {
this.getWeather(this.data.search_city)
},
/
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
this.getWeather("广州")
},
})
可能的错误原因
请复制上方错误信息到搜索引擎以获取关于该错误的说明,或点击「使用必应搜索」。
如果您是访客,这说明网站程序可能出现了一些错误。请您稍后再试,或联系站长。
如果您是站长,可以「点击这里」查看 Z-Blog 官方对于【部分常见错误 】的说明,,以及「通用排查指南」。
如果仍然无法解决,也可以到 Z-Blog 官方论坛,附上当前错误信息与描述寻求帮助。
注:请将"当前错误信息"复制进标题或正文中。
到此这篇天气预报接口源码(天气api接口源码)的文章就介绍到这了,更多相关内容请继续浏览下面的相关
推荐文章,希望大家都能在编程的领域有一番成就!
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/hd-api/12734.html