開發天氣預報小程序


實現效果

先來個效果圖

小程序開發:手把手教你開發天氣預報小程序

效果圖

3:項目結構

小程序開發:手把手教你開發天氣預報小程序

結構

4:小程序配置

使用app.json文件來對微信小程序進行全局配置,決定頁面文件的路徑、窗口表現、設置網絡超時時間、設置多 tab 等。

{

"pages":[

"pages/index/index"

],

"window":{

"backgroundTextStyle":"light",

"navigationBarBackgroundColor": "#fff",

"navigationBarTitleText": "天氣預報",

"navigationBarTextStyle":"black"

},

"networkTimeout": {

"request": 10000

},

"debug": true

}

由於項目只有一個頁面,所以不需要底部tab。另外設置網絡請求時間為10秒,並且啟用調試模式。

5:小程序邏輯層

首先在common.js中使用獲取用戶當前地理位置接口獲取用戶的坐標地址,坐標類型選擇gcj02。

//獲取當前位置坐標

function getLocation(callback) {

wx.getLocation({

type: 'gcj02',

success: function(res) {

callback(true, res.latitude, res.longitude);

},

fail: function() {

callback(false);

}

})

}

Wx.getlocation調用成功之后,將坐標信息返回給callback函數。失敗時將false傳給callback函數。

獲取到坐標之后,再使用百度接口查詢天氣。相應的查詢代碼如下所示。

function getWeather(latitude, longitude, callback) {

var ak = "";//換成自己的ak,

var url = "https://api.map.baidu.com/telematics/v3/weather?location=" + longitude + "," + latitude + "&output=json&ak=" + ak;

wx.request({

url: url,

success: function(res){

console.log(res);

callback(res.data);

}

});

}

在上述代碼中,先定義百度接口的ak,再通過拼接參數構造url的其他部分。然后調用 wx.request 請求天氣預報數據。

接下來把上述接口組合起來,組成給應用層的接口,相應代碼如下所示。

function loadWeatherData(callback) {

getLocation(function(success, latitude, longitude){

getWeather(latitude, longitude, function(weatherData){

callback(weatherData);

});

});

}

最后通過 module.exports對外暴露該接口。代碼如下所示。

module.exports = { loadWeatherData: loadWeatherData}

6:小程序頁面與視圖

//index.js

var common = require('common.js')

Page({

data: {

weather: {}

},

onLoad: function () {

var that = this;

common.loadWeatherData(function(data){

that.setData({

weather: data

});

});

}

})

在頁面的Page函數中, data定義為天氣的初始化數據,該數據將會以 JSON 的形式由邏輯層傳至渲染層。在 onLoad 方法中,使用common中的 loadWeatherData 方法獲取天氣數據並設置到 UI 上,並將取到的數據使用 setData 方法將它設置到數據層中。

在頁面的界面實現中,相應的代碼如下所示。

<!--index.wxml-->

<view class="container">

<view class="header">

<view class="title">{{weather.results[0].currentCity}}</view>

<view class="desc">{{weather.date}}</view>

</view>

<view class="menu-list">

<view class="menu-item" wx:for="{{weather.results[0].weather_data}}" wx:key="*this">

<view class="menu-item-main">

<text class="menu-item-name">{{item.date}} {{item.weather}} {{item.wind}} {{item.temperature}}</text>

<image class="menu-item-arrow" src="{{item.dayPictureUrl}}"></image>

</view>

</view>

</view>

</view>

最外層是一個 class 為 container 的 View,它的里面放了2個子 View,分別用於存放頁面頭和頁面列表。頁面頭中存放城市名稱和時間。頁面列表用於存放最近幾天的天氣情況。

頁面的樣式表實現如下所示。

.header {

padding: 30rpx;

line-height: 1;

}

.title {

font-size: 52rpx;

}

.desc {

margin-top: 10rpx;

color: #888888;

font-size: 28rpx;

}

.menu-list {

display: flex;

flex-direction: column;

background-color: #fbf9fe;

margin-top: 10rpx;

}

.menu-item {

color: #000000;

display: flex;

background-color: #fff;

margin: 10rpx 30rpx;

flex-direction: column;

}

.menu-item-main {

display: flex;

padding: 40rpx;

border-radius: 10rpx;

align-items: center;

font-size: 32rpx;

justify-content: space-between;

}

.menu-item-arrow {

width: 96rpx;

height: 96rpx;

transition: 400ms;

}

7:最終效果上圖

小程序開發:手把手教你開發天氣預報小程序


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM