由于疫情的关系不能出门,所以在家学习微信小程序的开发,利用百度天气预报API接口来获取天气数据,折腾了一天,总是获取不到天气数据,搞得我一头雾水。。。
今天我下载了百度的demo程序,利用里面提供的JavaScript API模块可以获取到正确的数据,然后我就对比我的代码和API模块的代码,发现竟然是传递的参数location的问题,模块的location = longitude<经度>,latitude<纬度>,竟然是经度在前,纬度在后!!!而别的比如逆地址解析、POI检索正好相反,location = latitude<纬度>,longitude<经度>。原来我调试过逆地址解析能得到数据,所以我以为天气API接口参数loaction也是一样的,没想到是反的,郁闷!
简单的代码如下:
//获取应用实例
const app = getApp();
Page({
data: {
},
//事件处理函数
onLoad: function () {
var that = this;
that.getlocation();
},
getlocation: function () {
var that = this;
wx.getLocation({
type: 'wgs84',
success(res) {
const latitude = res.latitude
const longitude = res.longitude