// const app = getApp(); // const api = require('../../../api/index.js'); const QQMapWX = require('../../../libs/qqmap-wx-jssdk.min.js'); // 引入QQ地图SDK(需自行下载) // this.qqmapsdk = require('../../../libs/qqmap-wx-jssdk.min.js'); // 初始化QQ地图API(需在腾讯地图开放平台申请key) const qqmapsdk = new QQMapWX({ key: 'VRGBZ-ZFRHB-SKIUP-NHHJ3-TXGJT-ZIFG3' // 替换为自己的API密钥(https://lbs.qq.com/dev/console/application/mine) }); Page({ data: { latitude: 0, // 纬度 longitude: 0, // 经度 searchKey: '', // 搜索关键词 poiList: [], // 附近地点列表 markers: [], // 地图标记点 loading: false // 加载状态 }, // 保持原有逻辑不变,仅需确保接收省市区参数正常 onLoad(options) { if (options.params) { try { this.setData({ prevParams: JSON.parse(decodeURIComponent(options.params)) }); } catch (err) { console.error('参数解析失败:', err); } } this.getLocation(); }, // 获取用户当前定位 getLocation() { wx.showLoading({ title: '获取位置中...' }); wx.getLocation({ type: 'gcj02', // 国测局坐标系(微信地图默认) success: (res) => { const { latitude, longitude } = res; this.setData({ latitude, longitude, // 添加当前位置标记点 markers: [{ id: 0, latitude, longitude, iconPath: 'https://esos-iot.com/myminio/project/0b0593293af54ea097b168cea479c25c.png', // 可自行添加定位图标 width: 30, height: 30, anchor: { x: 0.5, y: 0.5 } }] }); // 获取附近地点 this.getNearbyPoi(latitude, longitude); }, fail: (err) => { wx.hideLoading(); wx.showToast({ title: '获取定位失败,请开启定位权限', icon: 'none', duration: 2000 }); // 若定位失败,使用上一页已选地址的经纬度(如果有) const { province, city, district } = this.data.prevFormData; if (province && city && district) { this.geocoder(`${province}${city}${district}`); } } }); }, // 地址转经纬度(地理编码) geocoder(address) { qqmapsdk.geocoder({ address: address, success: (res) => { if (res.result && res.result.location) { const { lat, lng } = res.result.location; this.setData({ latitude: lat, longitude: lng, markers: [{ id: 0, latitude: lat, longitude: lng, iconPath: 'https://esos-iot.com/myminio/project/0b0593293af54ea097b168cea479c25c.png', width: 30, height: 30, anchor: { x: 0.5, y: 0.5 } }] }); this.getNearbyPoi(lat, lng); } }, fail: () => { wx.showToast({ title: '地址解析失败', icon: 'none' }); } }); }, // 获取附近地点(POI搜索) getNearbyPoi(latitude, longitude) { this.setData({ loading: true }); qqmapsdk.search({ keyword: this.data.searchKey || '小区,商铺,写字楼,学校,医院', // 默认搜索关键词 location: `${latitude},${longitude}`, radius: 2000, // 搜索半径(2000米) page_size: 20, // 每页结果数 success: (res) => { wx.hideLoading(); this.setData({ poiList: res.data, loading: false }); }, fail: (err) => { wx.hideLoading(); this.setData({ loading: false }); wx.showToast({ title: '获取附近地点失败', icon: 'none' }); } }); }, // 搜索输入事件 handleSearchInput(e) { this.setData({ searchKey: e.detail.value }); }, // 搜索确认 handleSearchConfirm() { const { searchKey, latitude, longitude } = this.data; if (searchKey.trim()) { // 关键词搜索 this.getNearbyPoi(latitude, longitude); } }, // 地图视野变化事件(拖动地图后刷新附近地点) handleRegionChange(e) { if (e.type === 'end' && e.causedBy === 'drag') { const { latitude, longitude } = this.data.mapCtx.getCenterLocation(); this.setData({ latitude, longitude }); this.getNearbyPoi(latitude, longitude); } }, // 点击地图标记点 handleMarkerTap(e) { const { id } = e.markerId; const { poiList } = this.data; if (id > 0 && poiList[id - 1]) { this.selectPoi({ currentTarget: { dataset: { poi: poiList[id - 1] } } }); } }, // 选择地点 selectPoi(e) { console.log(e); const selectedPoi = e.currentTarget.dataset.poi; if (!selectedPoi) return; // 解析地址信息(省市区+详细地址) const {ad_info,address, title, location } = selectedPoi; const detailAddress = address + title; // 组装地址数据addressDetails const addressData = { province:ad_info.province, city:ad_info.city, district:ad_info.district, addressDetails: detailAddress, latitude: location.lat, longitude: location.lng, poiName: title // 标记点名称(如小区名、商铺名) }; console.log(addressData); // 返回上一页并传递选择的地址数据 const pages = getCurrentPages(); const prevPage = pages[pages.length - 2]; if (prevPage) { prevPage.setData({ 'formData.province': ad_info.province, 'formData.city': ad_info.city, 'formData.district': ad_info.district, 'formData.addressDetails': detailAddress, 'formData.poiName': title, 'formData.latitude': location.lat, 'formData.longitude': location.lng }); } wx.navigateBack({ delta: 1 }); } });