index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //index.js
  2. //获取应用实例
  3. const app = getApp();
  4. var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
  5. var qqmapsdk;
  6. Page({
  7. data: {
  8. motto: 'Hello World',
  9. userInfo: {},
  10. hasUserInfo: false,
  11. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  12. province: '',
  13. city: '',
  14. latitude: '',
  15. longitude: '',
  16. address: 'Hello World'
  17. },
  18. //事件处理函数
  19. bindViewTap: function() {
  20. wx.navigateTo({
  21. url: '../logs/logs'
  22. })
  23. },
  24. onLoad: function() {
  25. if (app.globalData.userInfo) {
  26. this.setData({
  27. userInfo: app.globalData.userInfo,
  28. hasUserInfo: true
  29. })
  30. } else if (this.data.canIUse) {
  31. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  32. // 所以此处加入 callback 以防止这种情况
  33. app.userInfoReadyCallback = res => {
  34. this.setData({
  35. userInfo: res.userInfo,
  36. hasUserInfo: true
  37. })
  38. }
  39. } else {
  40. // 在没有 open-type=getUserInfo 版本的兼容处理
  41. wx.getUserInfo({
  42. success: res => {
  43. app.globalData.userInfo = res.userInfo
  44. this.setData({
  45. userInfo: res.userInfo,
  46. hasUserInfo: true
  47. })
  48. }
  49. })
  50. };
  51. qqmapsdk = new QQMapWX({
  52. //这里自己的key秘钥进行填充
  53. key: '7KWBZ-WCFKI-2PPGL-5JPAP-EEOG6-52BOV'
  54. });
  55. },
  56. onShow: function() {
  57. let vm = this;
  58. vm.getUserLocation();
  59. },
  60. getUserLocation: function() {
  61. let vm = this;
  62. wx.getSetting({
  63. success: (res) => {
  64. console.log(JSON.stringify(res))
  65. // res.authSetting['scope.userLocation'] == undefined 表示 初始化进入该页面
  66. // res.authSetting['scope.userLocation'] == false 表示 非初始化进入该页面,且未授权
  67. // res.authSetting['scope.userLocation'] == true 表示 地理位置授权
  68. //非初始化且用户地理位置未授权的情况
  69. if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
  70. wx.showModal({
  71. title: '请求授权当前位置',
  72. content: '需要获取您的地理位置,请确认授权',
  73. success: function(res) {
  74. if (res.cancel) {
  75. wx.showToast({
  76. title: '拒绝授权',
  77. icon: 'none',
  78. duration: 1000
  79. })
  80. } else if (res.confirm) {
  81. wx.openSetting({
  82. success: function(dataAu) {
  83. if (dataAu.authSetting["scope.userLocation"] == true) {
  84. wx.showToast({
  85. title: '授权成功',
  86. icon: 'success',
  87. duration: 1000
  88. })
  89. //再次授权,调用wx.getLocation的API
  90. vm.getLocation();
  91. } else {
  92. wx.showToast({
  93. title: '授权失败',
  94. icon: 'none',
  95. duration: 1000
  96. })
  97. }
  98. }
  99. })
  100. }
  101. }
  102. })
  103. } else if (res.authSetting['scope.userLocation'] == undefined) {//初始化进入该页面
  104. //调用wx.getLocation的API
  105. vm.getLocation();
  106. } else {
  107. //调用wx.getLocation的API
  108. vm.getLocation();
  109. }
  110. }
  111. })
  112. },
  113. // 微信获得经纬度
  114. getLocation: function() {
  115. let vm = this;
  116. wx.getLocation({
  117. type: 'wgs84',
  118. success: function(res) {
  119. console.log(JSON.stringify(res))
  120. var latitude = res.latitude
  121. var longitude = res.longitude
  122. var speed = res.speed
  123. var accuracy = res.accuracy;
  124. vm.getLocal(latitude, longitude)
  125. },
  126. fail: function(res) {
  127. console.log('fail' + JSON.stringify(res))
  128. }
  129. })
  130. },
  131. // 获取当前地理位置
  132. getLocal: function(latitude, longitude) {
  133. let vm = this;
  134. qqmapsdk.reverseGeocoder({
  135. location: {
  136. latitude: latitude,
  137. longitude: longitude
  138. },
  139. success: function(res) {
  140. console.log(res);
  141. console.log(JSON.stringify(res));
  142. let province = res.result.ad_info.province
  143. let city = res.result.ad_info.city
  144. let address = res.result.address
  145. vm.setData({
  146. province: province,
  147. city: city,
  148. latitude: latitude,
  149. longitude: longitude,
  150. address: address
  151. })
  152. },
  153. fail: function(res) {
  154. console.log(res);
  155. },
  156. complete: function(res) {
  157. // console.log(res);
  158. }
  159. });
  160. },
  161. getUserInfo: function(e) {
  162. console.log(e)
  163. app.globalData.userInfo = e.detail.userInfo
  164. this.setData({
  165. userInfo: e.detail.userInfo,
  166. hasUserInfo: true
  167. })
  168. }
  169. })