首先微信公众平台开通权限
app.json
"permission":{
"scope.userLocation": {
"desc": "您的位置信息将用于获取周围门店信息"
}
},
"requiredPrivateInfos": [
"getLocation"
],
"sitemapLocation": "sitemap.json",
index.js
onLoad: function () {
this.getLocation()
},
getLocation() {
console.log('pppp')
var that = this;
wx.getLocation({
type: 'wgs84',
success (res) {
console.log(res)
const latitude = res.latitude
const longitude = res.longitude
const speed = res.speed
const accuracy = res.accuracy
console.log(res) //将获取到的经纬度信息输出到控制台以便检查
that.setData({ //将获取到的经度、纬度数值分别赋值给本地变量
latitude: res.latitude,
longitude: res.longitude,
location:res.latitude+','+res.longitude
})
console.log(res.latitude+','+res.longitude)
wx.setStorageSync('location', res.latitude+','+res.longitude)//arr是要存的数据
},
fail(err){
console.log(err);
}
})
},