电速宝
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // const app = getApp();
  2. // const api = require('../../../api/index.js');
  3. const QQMapWX = require('../../../libs/qqmap-wx-jssdk.min.js'); // 引入QQ地图SDK(需自行下载)
  4. // this.qqmapsdk = require('../../../libs/qqmap-wx-jssdk.min.js');
  5. // 初始化QQ地图API(需在腾讯地图开放平台申请key)
  6. const qqmapsdk = new QQMapWX({
  7. key: 'VRGBZ-ZFRHB-SKIUP-NHHJ3-TXGJT-ZIFG3' // 替换为自己的API密钥(https://lbs.qq.com/dev/console/application/mine)
  8. });
  9. Page({
  10. data: {
  11. latitude: 0, // 纬度
  12. longitude: 0, // 经度
  13. searchKey: '', // 搜索关键词
  14. poiList: [], // 附近地点列表
  15. markers: [], // 地图标记点
  16. loading: false // 加载状态
  17. },
  18. // 保持原有逻辑不变,仅需确保接收省市区参数正常
  19. onLoad(options) {
  20. if (options.params) {
  21. try {
  22. this.setData({
  23. prevParams: JSON.parse(decodeURIComponent(options.params))
  24. });
  25. } catch (err) {
  26. console.error('参数解析失败:', err);
  27. }
  28. }
  29. this.getLocation();
  30. },
  31. // 获取用户当前定位
  32. getLocation() {
  33. wx.showLoading({
  34. title: '获取位置中...'
  35. });
  36. wx.getLocation({
  37. type: 'gcj02', // 国测局坐标系(微信地图默认)
  38. success: (res) => {
  39. const { latitude, longitude } = res;
  40. this.setData({
  41. latitude,
  42. longitude,
  43. // 添加当前位置标记点
  44. markers: [{
  45. id: 0,
  46. latitude,
  47. longitude,
  48. iconPath: 'https://esos-iot.com/myminio/project/0b0593293af54ea097b168cea479c25c.png', // 可自行添加定位图标
  49. width: 30,
  50. height: 30,
  51. anchor: { x: 0.5, y: 0.5 }
  52. }]
  53. });
  54. // 获取附近地点
  55. this.getNearbyPoi(latitude, longitude);
  56. },
  57. fail: (err) => {
  58. wx.hideLoading();
  59. wx.showToast({
  60. title: '获取定位失败,请开启定位权限',
  61. icon: 'none',
  62. duration: 2000
  63. });
  64. // 若定位失败,使用上一页已选地址的经纬度(如果有)
  65. const { province, city, district } = this.data.prevFormData;
  66. if (province && city && district) {
  67. this.geocoder(`${province}${city}${district}`);
  68. }
  69. }
  70. });
  71. },
  72. // 地址转经纬度(地理编码)
  73. geocoder(address) {
  74. qqmapsdk.geocoder({
  75. address: address,
  76. success: (res) => {
  77. if (res.result && res.result.location) {
  78. const { lat, lng } = res.result.location;
  79. this.setData({
  80. latitude: lat,
  81. longitude: lng,
  82. markers: [{
  83. id: 0,
  84. latitude: lat,
  85. longitude: lng,
  86. iconPath: 'https://esos-iot.com/myminio/project/0b0593293af54ea097b168cea479c25c.png',
  87. width: 30,
  88. height: 30,
  89. anchor: { x: 0.5, y: 0.5 }
  90. }]
  91. });
  92. this.getNearbyPoi(lat, lng);
  93. }
  94. },
  95. fail: () => {
  96. wx.showToast({
  97. title: '地址解析失败',
  98. icon: 'none'
  99. });
  100. }
  101. });
  102. },
  103. // 获取附近地点(POI搜索)
  104. getNearbyPoi(latitude, longitude) {
  105. this.setData({ loading: true });
  106. qqmapsdk.search({
  107. keyword: this.data.searchKey || '小区,商铺,写字楼,学校,医院', // 默认搜索关键词
  108. location: `${latitude},${longitude}`,
  109. radius: 2000, // 搜索半径(2000米)
  110. page_size: 20, // 每页结果数
  111. success: (res) => {
  112. wx.hideLoading();
  113. this.setData({
  114. poiList: res.data,
  115. loading: false
  116. });
  117. },
  118. fail: (err) => {
  119. wx.hideLoading();
  120. this.setData({ loading: false });
  121. wx.showToast({
  122. title: '获取附近地点失败',
  123. icon: 'none'
  124. });
  125. }
  126. });
  127. },
  128. // 搜索输入事件
  129. handleSearchInput(e) {
  130. this.setData({ searchKey: e.detail.value });
  131. },
  132. // 搜索确认
  133. handleSearchConfirm() {
  134. const { searchKey, latitude, longitude } = this.data;
  135. if (searchKey.trim()) {
  136. // 关键词搜索
  137. this.getNearbyPoi(latitude, longitude);
  138. }
  139. },
  140. // 地图视野变化事件(拖动地图后刷新附近地点)
  141. handleRegionChange(e) {
  142. if (e.type === 'end' && e.causedBy === 'drag') {
  143. const { latitude, longitude } = this.data.mapCtx.getCenterLocation();
  144. this.setData({ latitude, longitude });
  145. this.getNearbyPoi(latitude, longitude);
  146. }
  147. },
  148. // 点击地图标记点
  149. handleMarkerTap(e) {
  150. const { id } = e.markerId;
  151. const { poiList } = this.data;
  152. if (id > 0 && poiList[id - 1]) {
  153. this.selectPoi({ currentTarget: { dataset: { poi: poiList[id - 1] } } });
  154. }
  155. },
  156. // 选择地点
  157. selectPoi(e) {
  158. console.log(e);
  159. const selectedPoi = e.currentTarget.dataset.poi;
  160. if (!selectedPoi) return;
  161. // 解析地址信息(省市区+详细地址)
  162. const {ad_info,address, title, location } = selectedPoi;
  163. const detailAddress = address + title;
  164. // 组装地址数据addressDetails
  165. const addressData = {
  166. province:ad_info.province,
  167. city:ad_info.city,
  168. district:ad_info.district,
  169. addressDetails: detailAddress,
  170. latitude: location.lat,
  171. longitude: location.lng,
  172. poiName: title // 标记点名称(如小区名、商铺名)
  173. };
  174. console.log(addressData);
  175. // 返回上一页并传递选择的地址数据
  176. const pages = getCurrentPages();
  177. const prevPage = pages[pages.length - 2];
  178. if (prevPage) {
  179. prevPage.setData({
  180. 'formData.province': ad_info.province,
  181. 'formData.city': ad_info.city,
  182. 'formData.district': ad_info.district,
  183. 'formData.addressDetails': detailAddress,
  184. 'formData.poiName': title,
  185. 'formData.latitude': location.lat,
  186. 'formData.longitude': location.lng
  187. });
  188. }
  189. wx.navigateBack({ delta: 1 });
  190. }
  191. });