1. 程式人生 > >微信小程式(十六)實戰——微信小程式的百度地圖的Api獲取地理位置

微信小程式(十六)實戰——微信小程式的百度地圖的Api獲取地理位置

使用百度地圖的api來獲取地位位置的資訊

申請到ak後,在我的應用裡就能檢視到

第二步:引入JS模組

第三步:編輯程式碼

.js

// 引用百度地圖微信小程式JSAPI模組 
var bmap = require('../../libs/bmap-wx.js');
Page({
  data: {
    weatherData: ''
  },
  onLoad: function () {
    var that = this;
    // 新建百度地圖物件 
    var BMap = new bmap.BMapWX({
      ak: 'pDUmMVsgKG422Vg8zW5UGWNBTUAvcw9e'
    });
    var fail = function (data) {
      console.log(data)
    };
    var success = function (data) {
      var weatherData = data.currentWeather[0];
      weatherData = '城市:' + weatherData.currentCity + '\n' + 'PM2.5:' + weatherData.pm25 + '\n' + '日期:' + weatherData.date + '\n' + '溫度:' + weatherData.temperature + '\n' + '天氣:' + weatherData.weatherDesc + '\n' + '風力:' + weatherData.wind + '\n';
      that.setData({
        weatherData: weatherData
      });
    }
    // 發起weather請求 
    BMap.weather({
      fail: fail,
      success: success
    });
  }
})

.wxml

<view class="weather"> 
  <text>{{weatherData}}</text> 
</view>

效果: