在App的onLaunch()调用wx.get location获取当前位置经纬度,设置为全局变量,在其他页面获取不了!!!是怎么回事!求解!!!
这样写,没错吧?PS:学习中!
App({
onLaunch: function () {
// 展示本地存储能力
var that=this
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
}
})
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting[‘scope.userLocation’]) {
wx.getLocation({
success: function (res) {
that.globalData.lat = res.latitude,
that.globalData.long = res.longitude
},
})
}
}
})
},
globalData: {
userInfo: null,
lat:null,
long:null
}
})
//获取应用实例
const app = getApp()
Page({
data: {
motto: ‘Hello World’,
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse(‘button.open-type.getUserInfo’),
lat: app.globalData.lat,
long: app.globalData.long
}
<view>{{lat}}</view>
<view>{{long}}</view>