// 判断用户是否拒绝地理位置信息授权,拒绝的话重新请求授权
getUserLocation: function () {
let that = this;
wx.getSetting({
success: (res) => {
console.log(JSON.stringify(res))
// res.authSetting['scope.userLocation'] == undefined 表示 初始化进入该页面
// res.authSetting['scope.userLocation'] == false 表示 非初始化进入该页面,且未授权
// res.authSetting['scope.userLocation'] == true 表示 地理位置授权
if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
wx.showModal({
title: '请求授权当前位置',
content: '需要获取您的地理位置,请确认授权',
success: function (res) {
if (res.cancel) {
wx.showToast({
title: '拒绝授权',
icon: 'none',
duration: 1000
})
} else if (res.confirm) {
wx.openSetting({
success: function (dataAu) {
if (dataAu.authSetting["scope.userLocation"] == true) {
wx.showToast({
title: '授权成功',
icon: 'success',
duration: 1000
})
//再次授权,调用wx.getLocation的API
that.getLocation();
} else {
wx.showToast({
title: '授权失败',
icon: 'none',
duration: 1000
})
}
}
})
}
}
})
} else if (res.authSetting['scope.userLocation'] == undefined) {
//调用wx.getLocation的API
that.getLocation();
}
else {
//调用wx.getLocation的API
that.getLocation();
}
}
})
},
// 获取定位当前位置的经纬度
getLocation: function () {
let that = this;
wx.getLocation({
type: 'wgs84',
success: function (res) {
let latitude = res.latitude
let longitude = res.longitude
app.globalData.lat = res.latitude;//
app.globalData.lng = res.longitude;//把onload定位时候的经纬度存到全局
let speed = res.speed
let accuracy = res.accuracy;
that.getLocal(latitude, longitude)
},
fail: function (res) {
console.log('fail' + JSON.stringify(res))
}
})
},
// 获取当前地理位置
getLocal: function (latitude, longitude) {
let that = this;
qqmapsdk.reverseGeocoder({
location: {
latitude: latitude,
longitude: longitude
},
success: function (res) {
let province = res.result.ad_info.province
let city = res.result.ad_info.city
let district = res.result.ad_info.district;
// 保存一下当前定位的位置留着后面重新定位的时候搜索附近地址用
app.globalData.currentLocation = district;
that.setData({
province: province,
city: city,
latitude: latitude,
longitude: longitude,
district: district
})
},
fail: function (res) {
console.log(res);
},
complete: function (res) {
// console.log(res);
}
});
},
小程序定位(授权)获取当前位置
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.确定要接入哪家地图平台的api(百度地图,高德地图),本案例中使用的是高德地图2.注册高德地图账号--到个人中...
- 由于微信小程序只提供了获取地理位置(经纬度)、速度的api,并没有获取地理位置的信息。因此,我们需要利用第三方地图...
- 底部附导航:小程序所有组件效果示例,供查阅。 1、小程序授权登录:登录方式一:code登录: 登录方式二:账号密码...