微信小程式開發相關記錄(2017.07.23)

暖楓無敵發表於2017-07-23

1、獲取位置的API


這裡是獲取到位置,並且開啟地圖定位到手機當前的經緯度座標處

wx.getLocation({
  type: 'gcj02', //返回可以用於wx.openLocation的經緯度
  success: function(res) {
    var latitude = res.latitude
    var longitude = res.longitude
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      scale: 28
    })
  }
})

2、發起網路請求的API


const requestTask = wx.request({
  url: 'test.php', //僅為示例,並非真實的介面地址
  data: {
     x: '' ,
     y: ''
  },
  header: {
      'content-type': 'application/json'
  },
  success: function(res) {
    console.log(res.data)
  }
})

requestTask.abort() // 取消請求任務


3、根據經緯度座標獲取城市資訊的介面地址(百度API):

http://api.map.baidu.com/geocoder/v2/?ak=百度API Key&location=32.12,120.5&output=json


4、根據城市名稱獲取天氣資訊的介面地址:

http://wthrcdn.etouch.cn/weather_mini?city=合肥



5、頁面js方法呼叫,在微信API方法內呼叫另一個方法,要首先獲取到當前的頁面,如下圖所示:

var page = this;


6、獲取返回的Json值後,可以在頁面中直接.出對應節點的值,如下圖所示:

首先建立陣列



7、使用模板API,定義與引用




建立模板



8、利用迴圈獲取模板並展示


相關文章