小程序如何獲取當(dāng)前的天氣預(yù)報(bào)

大家好,我是陳楠酒肆,今天我為大家分享的是小程序獲取當(dāng)前的天氣預(yù)報(bào),我們先看看效果圖

效果圖

在實(shí)現(xiàn)這個(gè)效果之前我們需要引用一個(gè)JS文件,就是amap-wx.js,這個(gè)文件可以在我的交流群里下載。由于這里我使用了高德地圖密鑰,因此,大家還需要在高德網(wǎng)站上注冊(cè)一個(gè)賬號(hào),并獲取相應(yīng)的密鑰,有關(guān)這點(diǎn),我會(huì)單獨(dú)拉出來將,在此只做拋磚引玉。
好了,下面進(jìn)入正題,假設(shè)大家有了高德地圖的密鑰,那么下面我們?cè)撊绾螌?shí)現(xiàn)呢。
JS文件如下:

const amapFile = require('../../utils/amap-wx.js'); //引用amap-wx.js文件
const app = getApp();
 
Page({
 
  /**
   * 頁面的初始數(shù)據(jù)
   */
  data: {
    weather: [], //天氣對(duì)象
    markersData: {
      latitude: '',//緯度
      longitude: '',//經(jīng)度
      key: "你的密鑰"http://申請(qǐng)的高德地圖key
    },
 city:'',
    address:'',
    address2:'', 
  },
 
  /**
   * 生命周期函數(shù)--監(jiān)聽頁面加載
   */
  onLoad: function (options) {
    that.loadInfo(); //執(zhí)行函數(shù)
  },
 
  //獲取當(dāng)前位置的經(jīng)緯度
  loadInfo: function () {
    var that = this;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的經(jīng)緯度
      success: function (res) {
        var latitude = res.latitude//維度
        var longitude = res.longitude//經(jīng)度
        that.loadCity(latitude, longitude);
      }
    })
  },
  //把當(dāng)前位置的經(jīng)緯度傳給高德地圖,調(diào)用高德API獲取當(dāng)前地理位置,天氣情況等信息
  loadCity: function (latitude, longitude) {
    var that = this;
    var myAmapFun = new amapFile.AMapWX({ key: that.data.markersData.key });
    myAmapFun.getRegeo({
      location: '' + longitude + ',' + latitude + '',//location的格式為'經(jīng)度,緯度'
      success: function (data) {
        console.log("開始數(shù)據(jù)");
        console.log(data[0]['regeocodeData']['addressComponent']['city']);
        that.setData({
          city: data[0]['regeocodeData']['addressComponent']['city'],
          address:data[0]['desc'],
          address2: data[0]['name']
        });
        console.log(data);
      },
      fail: function (info) { }
    });
 
    myAmapFun.getWeather({
      success: function (data) {
        that.setData({
          weather: data
        })
        console.log("開始數(shù)據(jù)2");
        console.log(data);
        //成功回調(diào)
      },
      fail: function (info) {
        //失敗回調(diào)
        console.log("開始數(shù)據(jù)3");
        console.log(info)
      }
    })
  },
  /**
   * 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成
   */
  onReady: function () {
    
  },
 
  /**
   * 生命周期函數(shù)--監(jiān)聽頁面顯示
   */
  onShow: function () {
    
  },
 
  /**
   * 生命周期函數(shù)--監(jiān)聽頁面隱藏
   */
  onHide: function () {
    
  },
 
  /**
   * 生命周期函數(shù)--監(jiān)聽頁面卸載
   */
  onUnload: function () {
    
  },
 
  /**
   * 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動(dòng)作
   */
  onPullDownRefresh: function () {
    
  },
 
  /**
   * 頁面上拉觸底事件的處理函數(shù)
   */
  onReachBottom: function () {
    
  },
 
  /**
   * 用戶點(diǎn)擊右上角分享
   */
  onShareAppMessage: function () {
    
  }
})

wxml文件如下:

<view>所屬城市:{{weather.city.data}}</view>
<view>城市濕度:{{weather.humidity.data}}</view>
<view>城市溫度:{{weather.temperature.data}}</view>
<view>城市天氣:{{weather.weather.data}}</view>
<view>城市風(fēng)向:{{weather.winddirection.data}}</view>
<view>城市風(fēng)力:{{weather.windpower.data}}</view>

以上就是核心內(nèi)容,小程序運(yùn)行起來的效果是:

image

以上就是核心代碼,有什么不明白的地方可以給我留言。

?著作權(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)容

  • 轉(zhuǎn)載鏈接 注:本文轉(zhuǎn)載知乎上的回答 作者:初雪 鏈接:https://www.zhihu.com/question...
    pengshuangta閱讀 29,332評(píng)論 9 295
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,688評(píng)論 19 139
  • 每天的學(xué)習(xí)記錄,可能有的地方寫的不對(duì),因?yàn)閯倢W(xué),以后發(fā)現(xiàn)錯(cuò)的話會(huì)回來改掉整體流程 https://develope...
    有點(diǎn)健忘閱讀 5,052評(píng)論 0 7
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,323評(píng)論 25 708
  • 杳杳寒山路,落落冷澗濱。 啾啾常有鳥,寂寂更無人。 淅淅風(fēng)吹面,紛紛雪積身。 朝朝不見日,歲歲不知春。 唐代有名的...
    吉草末閱讀 321評(píng)論 0 0

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