获取当前的地理位置、速度可以用wx.getLocation
但当用户离开小程序后,此接口无法调用;当用户点击“显示在聊天顶部”时,此接口可继续调用。
OBJECT参数说明:
| 参数 | 类型 | 必填 | 说明 | 最低版本 |
|---|---|---|---|---|
| type | String | 否 | 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于wx.openLocation的坐标 | |
| altitude | Boolean | 否 | 传入 true 会返回高度信息,由于获取高度需要较高精确度,会减慢接口返回速度 | 1.6.0 |
| success | Function | 是 | 接口调用成功的回调函数,返回内容详见返回参数说明。 | |
| fail | Function | 否 | 接口调用失败的回调函数 | |
| complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
如果你想获取你所在的地理位置可以如下写:
<button bindtap='mylocation'>获取定位</button>
<view>经度:{{latitude}}</view>
<view>维度:{{longitude}}</view>
js:
mylocation:function(e){
wx.getLocation({
type: 'wgs84',
success: (res)=> {
console.log(res)
this.setData({
latitude:res.latitude,
longitude: res.longitude
})
wx.showModal({
title: '当前坐标',
content: '维度:'+ res.latitude +',经度:'+ res.longitude,
})
},
})
},
本文介绍如何使用微信小程序API wx.getLocation 获取用户的地理位置信息,并展示了具体的代码实现。该方法可以在用户将小程序保持在前台时调用,返回GPS坐标或适用于wx.openLocation的坐标。
1万+

被折叠的 条评论
为什么被折叠?



