//獲取位置 getSelfLocation: function (varSendOrgId) { var that = this;
//用戶授權過可以直接獲取位置 wx.getLocation({ //type: 'wgs84', type: 'gcj02', success: function (res) { var latitude = res.latitude; var longitude = res.longitude; that.setData({ selfLongitude: longitude,//經度 selfLatitude: latitude,//緯度 }); }, fail: function (res) { setTimeout(function () {
//需要用戶授權獲取位置 wx.getSetting({ success: (res) => { if (!res.authSetting['scope.userLocation']) { wx.openSetting({ success: (response) => { wx.showModal({ title: '請重新授權', content: '需要獲取您的地址信息', success: function (data) { wx.getLocation({ success: function (res) { var latitude = res.latitude; var longitude = res.longitude; that.setData({ selfLongitude: longitude,//經度 selfLatitude: latitude,//緯度 }); } }) } }); } }) } } }) }, 1000); } }); },
經過測試,使用GCJ02(火星坐標)比WGS84(GPS)的坐標在計算距離的時候更為精確