果玩软件园:为用户提供海量热门软件、游戏等手机资源下载服务!

装机必备热门标签玩游戏装软件BT游戏H5游戏看教程专题游戏盒子手机版

果玩软件园

所在位置:首页 > 资讯教程 > 软件教程 >  > 详情

如何使用微信小程序实现单页面的天气预报?

文章来源:网络作者:忘爱。发布时间:2026-05-29 01:29:20

GM盒子
GM盒子(高返利版)
GM手游福利平台,免费送首充,上线送VIP,免费领元宝和代金券。
Ready

  小编今天给大家带来的是如何使用微信小程序实现单页面天气预报?下面就和小编一起来学习一下吧,希望能够帮助到大家。

  1.先look下效果图

  效果图.png

  2.思路分析

  2.1布局

  2.2调用接口

  2.3把接口显示在界面上

  main.wxml

  {{today.wendu}}℃

  {{today.todayInfo.type}} {{today.todayInfo.fengxiang}}

  友情提示: {{today.ganmao}}

  {{newCity}}

  注意:在编辑HTML文件的时候,我们先把文件里面的框架先打起来,然后在去写CSS,这样才能写出优雅。

  main.wxss

  .content{

  font-family: Cambria, Cochin, Georgia, Times, Times New Roman, serif;

  font-size: 14px;

  background-size: cover;

  width: 100%;

  height: 100%;

  background: url(../assets/images/bg.jpeg);

  color: #fff;

  }

  .info{

  padding: 20px;

  background: #fff;

  background: rgba(0, 0, 0, .5);

  box-shadow: 10px,10px,20px,rgba(0, 0, 0, .5);

  border-radius: 5px;

  }

  .today{

  padding-top: 30rpx;

  height: 50%;

  }

  .temp{

  font-size: 50px;

  text-align: center;

  }

  .weather{

  text-align: center;

  margin-bottom: 20rpx;

  }

  .city{

  color: white;

  font-size: 16px;

  text-align: right;

  margin-right: 10rpx;

  margin-top: 30rpx;

  }

  .future{

  display: flex;

  flex-direction: row;

  height: 60%;

  padding-left: 5rpx;

  background:#FFFFFF;

  background:rgba(0, 0, 0, 0.1);

  box-shadow:10px 10px 20px rgba(0,0,0,0.5);

  text-align: center;

  padding-top:10rpx;

  padding-bottom:20rpx;

  margin-top: 270rpx;

  margin-bottom: 200rpx;

  }

  .future-item{

  min-height:320rpx;

  width: 165rpx;

  margin-left: 10rpx;

  margin-right: 10rpx;

  border: 1px solid coral;

  border-radius:10px;

  padding-top:10rpx;

  }

  .future-item view{

  margin-top:10px;

  }

  这个就没有什么可说的了,就是css样式。

  main.js

  Page({

  data:{

  newCity:" ",

  },

  onLoad:function(options){

  // 页面初始化 options为页面跳转所带来的参数

  this.loadInfo();

  },

  onReady:function(){

  // 页面渲染完成

  },

  onShow:function(){

  // 页面显示

  },

  onHide:function(){

  // 页面隐藏

  },

  onUnload:function(){

  // 页面关闭

  },

  loadInfo:function(){

  var _this=this;

  wx.getLocation({

  type: \'gcj02\', //返回可以用于wx.openLocation的经纬度

  success: function(res) {

  var latitude = res.latitude

  var longitude = res.longitude

  console.log(latitude+"+++"+longitude);

  _this.loadCity(latitude,longitude);

  }

  })

  },

  loadCity:function(latitude,longitude){

  var _this=this;

  wx.request({

  url: \'//api.map.baidu.com/geocoder/v2/?ak=NRdxFogcgGCystGBQBmULnfaHVikzfyk&location=\'+latitude+","+longitude+"&output=json", //仅为示例,并非真实的接口地址

  header: {

  \'content-type\': \'application/json\'

  },

  success: function(res) {

  console.log(res);

  var City=res.data.result.addressComponent.city;

  // console.log(City);

  City=City.replace("市","")

  _this.setData({newCity:City});

  _this.loadWeather(City);

  }

  })

  },

  loadWeather:function(newcity){

  var _this=this;

  wx.request({

  url: \'//wthrcdn.etouch.cn/weather_mini?city=\'+newcity, //仅为示例,并非真实的接口地址

  header: {

  \'content-type\': \'application/json\'

  },

  success: function(res) {

  console.log(res);

  var future=res.data.data.forecast;

  var todayInfo=future.shift();

  var today=res.data.data;

  today.todayInfo=todayInfo;

  _this.setData({future:future,today:today});

  }

  })

  }

  })

  在业务逻辑层上,要这样考虑:获取天气信息,需要获得定位地址;如何获取定位地址?找到定位地址的经纬度?那么如何找到经纬度呢?通过ip?还是有其他方式?经查找百度地图有提供现成的api接口?

  先想要实现一个功能,你需要什么东西,然后再想着如何获得这些东西!

  以上就是如何使用微信小程序实现单页面的天气预报的全部内容了,大家都学会了吗?

End
复制本文链接资讯文章为果玩软件园所有,未经允许不得转载。
热门游戏MORE+
相关资讯MORE+
最新录入
热门资讯
新游新品榜
手机游戏
休闲益智
赛车竞速
棋牌桌游
角色扮演
动作射击
体育竞技
经营养成
策略塔防
冒险解谜
音乐游戏
手游辅助
H5游戏
BT游戏
手机软件
社交聊天
系统工具
时尚购物
旅游出行
影音播放
生活实用
办公学习
资讯阅读
拍摄美化
游戏辅助
健康医疗
地图导航
小说漫画
安全防护
育儿亲子
手游下载
梦想三国之勇往直前0....
炼仙传说0.1折
不可思议的刀剑与魔法...
逍遥浪人
奇幻梦旅人
玄影0.1折
点击冒险之旅(0.1折特...
天神赵子龙0.1折
九州异兽记0.1折
龙魂魔法0.1折
装机软件
爱奇艺电脑版
Steam下载管家 2026最...
360游戏大厅
GoLink加速器
3DM驱动大师
夸克
豆包电脑版
360C盘扩容大师
360录屏
360极速浏览器
精选专题
手机游戏专题
手机软件专题
电脑软件专题
电脑游戏专题
游戏排行榜
手游排行榜
软件排行榜
BT排行榜
电脑软件排行榜
电脑游戏排行榜