在微信小程序中调用第三方API需要使用wx.request()
方法发送HTTP请求。首先需要在小程序的app.json
文件中添加"permission":{"scope.userLocation":{}}
以获取用户授权,然后在页面中使用wx.getLocation()
方法获取用户位置信息。接着,使用wx.request()
方法发送请求,并在回调函数中处理返回的数据。以下是一个示例代码:
wx.getLocation({
type: 'gcj02', // 坐标类型
success(res) {
const latitude = res.latitude // 纬度
const longitude = res.longitude // 经度
wx.request({
url: 'https://api.example.com', // API地址
data: {
latitude: latitude,
longitude: longitude
},
success(res) {
console.log(res.data) // 打印返回的数据
}
})
}
})