小程序?qū)崿F(xiàn)單頁面的天氣預(yù)報(bào)

1.先look下效果圖


效果圖.png

2.思路分析
2.1布局
2.2調(diào)用接口
2.3把接口顯示在界面上
main.wxml

<view class="content">
    <view class="today">
        <view class="info">
            <view class="temp">  {{today.wendu}}℃ </view>
            <view class="weather"> {{today.todayInfo.type}} {{today.todayInfo.fengxiang}} {{today.todayInfo.fengli}}</view>
            <view > 友情提示: {{today.ganmao}}</view>
            <view class="city"> {{newCity}}</view>
        </view>
    </view>

<import src="../template/itemtpl"/>
<view class="future" >
    <block wx:for="{{future}}">
         <template is="future-item" data="{{item}}"/>
    </block>
 </view>

</view>

注意:在編輯HTML文件的時(shí)候,我們先把文件里面的框架先打起來,然后在去寫CSS,這樣才能寫出優(yōu)雅。
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;
}

這個(gè)就沒有什么可說的了,就是css樣式。
main.js

Page({
  data:{
  newCity:" ",
  },
  onLoad:function(options){
    // 頁面初始化 options為頁面跳轉(zhuǎn)所帶來的參數(shù)
   this.loadInfo();
  },
  onReady:function(){
    // 頁面渲染完成
   
  },
  onShow:function(){
    // 頁面顯示
    
  },
  onHide:function(){
    // 頁面隱藏
   
  },
  onUnload:function(){
    // 頁面關(guān)閉
   
  },
  loadInfo:function(){
      var _this=this;
    wx.getLocation({
    type: 'gcj02', //返回可以用于wx.openLocation的經(jīng)緯度
    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: 'http://api.map.baidu.com/geocoder/v2/?ak=NRdxFogcgGCystGBQBmULnfaHVikzfyk&location='+latitude+","+longitude+"&output=json", //僅為示例,并非真實(shí)的接口地址
  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: 'http://wthrcdn.etouch.cn/weather_mini?city='+newcity, //僅為示例,并非真實(shí)的接口地址
        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});
        }
        })
    }
})

在業(yè)務(wù)邏輯層上,要這樣考慮:獲取天氣信息,需要獲得定位地址;如何獲取定位地址?找到定位地址的經(jīng)緯度?那么如何找到經(jīng)緯度呢?通過ip?還是有其他方式?經(jīng)查找百度地圖有提供現(xiàn)成的api接口?
先想要實(shí)現(xiàn)一個(gè)功能,你需要什么東西,然后再想著如何獲得這些東西!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容