address.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // const key = 'INCBZ-QYVK3-72F3B-RMOA2-OOIZ2-63BSR'; // 使用在腾讯位置服务申请的key
  2. // const referer = '红孩儿测试'; // 调用插件的app的名称
  3. // const hotCitys = ['北京', '上海', '广州', '深圳']; // 用户自定义的的热门城市
  4. // const accurate = '1'; // 是否使用getLocation进行定位
  5. // "permission": {
  6. // "scope.userLocation": {
  7. // "desc": "你的位置信息将用于小程序定位"
  8. // }
  9. // },
  10. // "plugins": {
  11. // "citySelector": {
  12. // "version": "1.0.3",
  13. // "provider": "wx63ffb7b7894e99ae"
  14. // }
  15. // },
  16. Page({
  17. data: {
  18. latitude: 0, // 当前纬度
  19. longitude: 0, // 当前经度
  20. nearbyAddresses: [
  21. {
  22. name: '附近地址 1',
  23. address: '详细地址 1',
  24. checked: false
  25. },
  26. {
  27. name: '附近地址 2',
  28. address: '详细地址 2',
  29. checked: false
  30. }
  31. ], // 附近地址列表
  32. selectedAddress: null // 用户选中的地址
  33. },
  34. onShow() {
  35. // wx.navigateTo({
  36. // url: `plugin://citySelector/index?key=${key}&referer=${referer}&hotCitys=${hotCitys}&accurate=${accurate}`,
  37. // });
  38. },
  39. onLoad() {
  40. this.checkLocationAuthorization();
  41. },
  42. // 检查位置授权
  43. checkLocationAuthorization() {
  44. wx.getSetting({
  45. success: (res) => {
  46. if (!res.authSetting['scope.userLocation']) {
  47. // 用户未授权,请求授权
  48. wx.authorize({
  49. scope: 'scope.userLocation',
  50. success: () => {
  51. this.getLocation();
  52. },
  53. fail: () => {
  54. // 用户拒绝授权,提示用户手动开启位置服务
  55. wx.showModal({
  56. title: '提示',
  57. content: '请在设置中开启位置服务授权',
  58. showCancel: false,
  59. confirmText: '去设置',
  60. success: (res) => {
  61. if (res.confirm) {
  62. wx.openSetting({});
  63. }
  64. }
  65. });
  66. }
  67. });
  68. } else {
  69. // 用户已授权,直接获取位置信息
  70. this.getLocation();
  71. }
  72. }
  73. });
  74. },
  75. // 获取当前位置
  76. getLocation() {
  77. wx.getLocation({
  78. type: 'wgs84',
  79. success: (res) => {
  80. const latitude = res.latitude;
  81. const longitude = res.longitude;
  82. this.setData({ latitude, longitude });
  83. this.getNearbyAddresses(latitude, longitude);
  84. },
  85. fail: (err) => {
  86. console.error('Failed to get location:', err);
  87. wx.showToast({
  88. title: '获取位置失败',
  89. icon: 'none'
  90. });
  91. }
  92. });
  93. },
  94. // 获取附近地址
  95. getNearbyAddresses(latitude, longitude) {
  96. const apiKey = 'INCBZ-QYVK3-72F3B-RMOA2-OOIZ2-63BSR'; // 替换为你的 API Key
  97. const url = `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${apiKey}`;
  98. wx.request({
  99. url,
  100. success: (res) => {
  101. if (res.data.status === 0) {
  102. const address = res.data.result.address;
  103. const nearbyAddresses = [
  104. {
  105. name: '当前位置',
  106. address: address,
  107. checked: false
  108. },
  109. {
  110. name: '附近地址 1',
  111. address: '详细地址 1',
  112. checked: false
  113. },
  114. {
  115. name: '附近地址 2',
  116. address: '详细地址 2',
  117. checked: false
  118. }
  119. ];
  120. this.setData({ nearbyAddresses });
  121. } else {
  122. console.error('Failed to get nearby addresses:', res.data.message);
  123. }
  124. },
  125. fail: (err) => {
  126. console.error('Request failed:', err);
  127. }
  128. });
  129. },
  130. // 选择地址
  131. selectAddress(e) {
  132. const index = e.currentTarget.dataset.index;
  133. const nearbyAddresses = this.data.nearbyAddresses.map((item, i) => ({
  134. ...item,
  135. checked: i === index
  136. }));
  137. const selectedAddress = nearbyAddresses[index];
  138. this.setData({ nearbyAddresses, selectedAddress });
  139. },
  140. // 取消
  141. onCancel() {
  142. wx.navigateBack();
  143. },
  144. // 确认
  145. onConfirm() {
  146. const { selectedAddress } = this.data;
  147. if (selectedAddress) {
  148. const pages = getCurrentPages();
  149. const prevPage = pages[pages.length - 2]; // 上一页
  150. prevPage.setData({ selectedAddress }); // 将选中的地址传递给上一页
  151. wx.navigateBack();
  152. } else {
  153. wx.showToast({
  154. title: '请选择一个地址',
  155. icon: 'none'
  156. });
  157. }
  158. }
  159. });