1. 程式人生 > >小程序獲取用戶的地理位置與商家的相距距離

小程序獲取用戶的地理位置與商家的相距距離

toast req 地理 gets mat itl math.sqrt ces 拒絕

getUserLocation: function () { let vm = 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 vm.getLocation(); } else { wx.showToast({ title: ‘授權失敗‘, icon: ‘none‘, duration: 1000 }) } } }) } } }) } else if (res.authSetting[‘scope.userLocation‘] == undefined) { //調用wx.getLocation的API vm.getLocation(); } else { //調用wx.getLocation的API vm.getLocation(); } } }) }, // 獲取用戶的當前位置 getLocation: function () { let that= this; wx.getLocation({
type: ‘wgs84‘, success: function (res) { var la1 = res.latitude var lo2 = res.longitude that.setData({ la1: la1, lo2: lo2, scale:28 }) that.getShangjia(la1, lo2); }
}) }, distance: function (la1, lo1, la2, lo2) { var La1 = la1 * Math.PI / 180.0; var La2 = la2 * Math.PI / 180.0; var La3 = La1 - La2; var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0; var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2))); s = s * 6378.137;//地球半徑 s = Math.round(s * 10000) / 10000; console.log("計算結果", s) this.setData({ s: s }) }, //商家 getShangjia: function (la1, lo2) { var that = this; app.util.request({ ‘url‘: ‘entry/wxapp/Shangjia‘, success: function (res) { console.log(res.data.data); var shangjia = res.data.data for (var i = 0; i < shangjia.length; i++) { // la2 = shangjia[i].s_latitude, // lo2 = shangjia[i].s_longitude that.distance(la1, lo2, shangjia[i].s_latitude, shangjia[i].s_longitude) shangjia[i].s_shopjuli = that.data.s; } that.setData({ shangjia: res.data.data }) }, fail: function (err) { console.log(err) }, }); },
// // 獲取用戶的當前位置 // wx.getLocation({ // type: ‘wgs84‘, // success: function (res) { // var la1 = res.latitude // var lo2 = res.longitude // that.setData({ // la1: la1, // lo2: lo2, // scale:28 // }) // that.getShangjia(la1, lo2); // } // })

小程序獲取用戶的地理位置與商家的相距距離