關於小程序受權地理位置(wx.getLocation + 用戶體驗)

 

 

wx.getLocation 若是用戶曾點擊過一次 「確認受權」 , git

那麼再次調用該接口時將不會出現彈出框(能夠直接拿到經緯度)spa

 

 

關於用戶體驗:code

 

  在 onLoad 中判斷:blog

    若是用戶以前「沒有觸發過「受權地理位置事件,則發起請求受權-----> 若是贊成受權,直接拿到經緯度處理邏輯;接口

                                  若是不一樣意,則彈出自定義模態框進行提示,若是用戶贊成就打開setting頁面;若是還不一樣意,就行默認事件(用戶不受權的邏輯)事件

 

wx.getSetting({ success: function(res){ if (typeof (res.authSetting['scope.userLocation']) == "undefined") { // 從未觸發過受權的狀況 wx.getLocation({ // 用戶贊成受權地理位置 success: function (res) { that.setData({ latitude: res.latitude, longitude: res.longitude }); that.getShopInfo_nearby(res.latitude, res.longitude); }, fail: function (err) { // 用戶拒絕受權地理位置 wx.showModal({//彈出模態框,詢問 title: '是否受權當前位置', content: '如需正常使用本程序,請按肯定並在受權管理中選中「地理位置」,而後點按返回便可正常使用。', cancelColor: '#f00', success: function (res) { if (res.confirm) {//贊成受權 wx.openSetting({}) } else if (res.cancel) {//不一樣意受權,進行普通查詢 mac = '08:D8:33:7C:5E:5'; that.getShopInfo(mac); } } }) } }) } } })

 

 

在 onShow 中:先檢查用戶受權狀態,若是「曾經觸發過「地理位置受權事件,但沒有確認受權,就彈出自定義模態框進行詢問(贊成->setting頁面; 不一樣意-> 走不一樣意的邏輯);
get

                 若是受權過了,則直接使用wx.getLocation 獲取經緯度拿到經緯度處理邏輯(此時不會再觸發彈框)it

 

 

wx.getSetting({//檢測用戶權限 success: (res) => { if (res.authSetting['scope.userLocation'] == false) { // 地理位置受權 觸發過但沒有受權 wx.showModal({//彈出模態框,詢問 title: '是否受權當前位置', content: '如需正常使用紫燕到家設備,請按肯定並在受權管理中選中「地理位置」,而後點按返回便可正常使用。', cancelColor: '#f00', success: function (res) { if (res.confirm) {//贊成受權 wx.openSetting({}) } else if (res.cancel) {//不一樣意受權,進行普通查詢 mac = '08:D8:33:7C:5E:5'; that.getShopInfo(mac); } } }) } else if (res.authSetting['scope.userLocation'] == true){ // 地理位置受權過了 wx.getLocation({ success: function(res) { that.getShopInfo_nearby(res.latitude, res.longitude); }, }) } } })

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1io

相關文章
相關標籤/搜索