Location.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. let app = getApp();
  2. var QQMapWX = require("./qqmap-wx-jssdk.min.js");
  3. function checkGPS(t, fn=null) {
  4. wx.authorize({
  5. scope: "scope.userLocation",
  6. success: function () {
  7. console.log("get GPS success"), wx.getLocation({
  8. success: function (n) {
  9. console.log("get GPS location success"), app.globalData.location = {
  10. lat: n.latitude,
  11. lng: n.longitude
  12. }, app.globalData.canGetGPS = true, fn;
  13. wx.setStorage({
  14. key: "latitude",
  15. data: n.latitude
  16. })
  17. wx.setStorage({
  18. key: "longitude",
  19. data: n.longitude
  20. })
  21. },
  22. fail: function () {
  23. console.log("get GPS location fail"), app.globalData.canGetGPS = false, fn&&fn();
  24. }
  25. });
  26. },
  27. fail: function () {
  28. console.log("get GPS fail checkGPS"), app.globalData.canGetGPS = false, fn&&fn();
  29. }
  30. });
  31. }
  32. function openSetting(t) {
  33. return new Promise(function (resolve, reject) {
  34. wx.showModal({
  35. content: "为了更好的服务您,需要您的地理位置",
  36. confirmText: "去开启",
  37. confirmColor: "#FF673F",
  38. success: function(res1) {
  39. if(res1.confirm) {
  40. wx.openSetting({
  41. success: function(result) {
  42. console.log(result);
  43. if(result.authSetting["scope.userLocation"]){
  44. wx.getLocation({
  45. success: function(res) {
  46. console.log("get GPS location success");
  47. getApp().globalData.location = {
  48. lat: res.latitude,
  49. lng: res.longitude
  50. },
  51. getApp().globalData.canGetGPS = true;
  52. resolve(res);
  53. },
  54. fail: function (error) {
  55. console.log("get GPS fail openSetting");
  56. getApp().globalData.canGetGPS = false;
  57. reject('取消', error);
  58. }
  59. });
  60. } else {
  61. reject('未开启');
  62. }
  63. },
  64. fail: function(error) {
  65. reject(error);
  66. }
  67. });
  68. } else if(res1.cancel) {
  69. reject('用户点击取消');
  70. console.log('用户点击取消')
  71. }
  72. }
  73. });
  74. });
  75. }
  76. /**
  77. * 获取定位城市
  78. */
  79. function getGps() {
  80. let that = this;
  81. return new Promise(function (resolve, reject) {
  82. wx.getLocation({
  83. type: 'gcj02', //编码方式,
  84. success: function (res) {
  85. console.log("getGps-res", res);
  86. resolve(res);
  87. wx.setStorage({
  88. key: "latitude",
  89. data: res.latitude
  90. })
  91. wx.setStorage({
  92. key: "longitude",
  93. data: res.longitude
  94. })
  95. },
  96. fail: (error) => {
  97. console.log("getGps-error", error);
  98. if (error.errMsg == "getLocation:fail auth deny"){
  99. that.openSetting().then(function(res){
  100. console.log(res);
  101. // reject(res)
  102. }).catch(function(){
  103. console.log(error);
  104. reject(error);
  105. });
  106. } else {
  107. // reject(error);
  108. console.log(error);
  109. // reject(error);
  110. }
  111. }
  112. })
  113. })
  114. }
  115. /**
  116. * 通过GPS获取详细位置
  117. */
  118. function getGpsLocation(lat, lon) {
  119. var tx_map_key = wx.getStorageSync('tx_map_key');
  120. if (tx_map_key) {
  121. return new Promise(function (resolve, reject) {
  122. analyzeGps(tx_map_key, lat, lon).then((res)=>{
  123. resolve(res);
  124. });
  125. })
  126. } else {
  127. return new Promise(function (resolve, reject) {
  128. app.util.request({
  129. url: 'entry/wxapp/index',
  130. data: {
  131. controller: 'index.get_community_config'
  132. },
  133. dataType: 'json',
  134. success: function (res) {
  135. if (res.data.code == 0) {
  136. tx_map_key = res.data.tx_map_key;
  137. wx.setStorage({
  138. key: "tx_map_key",
  139. data: tx_map_key
  140. })
  141. analyzeGps(tx_map_key, lat, lon).then((res) => {
  142. resolve(res);
  143. });
  144. }
  145. }
  146. })
  147. })
  148. }
  149. }
  150. /**
  151. * 解析GPS
  152. */
  153. function analyzeGps(tx_map_key, lat, lon) {
  154. var demo = new QQMapWX({
  155. key: tx_map_key
  156. });
  157. return new Promise(function (resolve, reject) {
  158. demo.reverseGeocoder({
  159. //腾讯地图api 逆解析方法 首先设置经纬度
  160. location: {
  161. latitude: lat,
  162. longitude: lon
  163. }, //逆解析成功回调函数
  164. success: function (res) {
  165. console.log(res)
  166. let address_component = res.result.address_component || {};
  167. let address_reference = res.result.address_reference;
  168. address_component.town = address_reference.town&&address_reference.town.title || '';
  169. resolve(address_component);
  170. }
  171. })
  172. })
  173. }
  174. module.exports = {
  175. checkGPS,
  176. openSetting,
  177. getGps,
  178. getGpsLocation
  179. }