// 此处以小程序用户初次点击拒绝位置定位权限后,引导用户打开位置授权作为例子
chooseLoaction: function() { // 1、查询是否有位置权限 wx.getSetting({ success: res=>{ if (!res.authSetting['scope.userLocation']) { // 2.1、无权限,询问开启权限 wx.authorize({ scope: 'scope.userLocation', // 3.1、用户确定授权 success: ()=>{ this.selectLocation() }, // 3.2、用户拒绝授权 fail: ()=>{ wx.showModal({ title: '温馨提示', content: '你拒绝了位置授权,是否去设置打开权限', confirmText: '确定', cancelText: '取消', success: res=>{ // 4.1、打开权限设置 if(res.confirm) { wx.openSetting({ success: res=>{ res.authSetting = { "scope.userLocation": true } this.selectLocation() } }) } else { wx.showToast({ title: '请手动设置授权', icon: 'none' }) } } }) } }) } else{ // 2.2、有权限,则直接保存位置信息 this.selectLocation() } } }) }
selectLocation: function(){ wx.chooseLocation({ success: res=> { let location = [] // location.latitude = res.latitude // location.longitude = res.longitude location[0] = res.longitude location[1] = res.latitude this.setData({ location, address: res.name }) }, fail: err=>{ console.log(err) } }) }