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è)功能,你需要什么東西,然后再想著如何獲得這些東西!