1 line
3.4 KiB
Plaintext
1 line
3.4 KiB
Plaintext
{"version":3,"file":"mapService.js","sources":["../../../../src/service/mapService.js"],"sourcesContent":["import { toast } from '@/utils/commUtils'\nimport { httpGet } from '@/utils/http'\n\nconst CACHE_KEY = 'lastLocation'; // 缓存位置的键\nconst THROTTLE_TIME = 30000; // 30秒内不能重复获取\n\n// 通过高德地图api获取详细位置\nexport const getReverseGeocoding = async (longitude, latitude) => {\n try {\n const res = await uni.request({\n url: `https://restapi.amap.com/v3/geocode/regeo?location=${longitude},${latitude}&key=${config.AMapKey}`,\n })\n\n if (res[1].data.status === '1' && res[1].data.regeocode) {\n // const address = res[1].data.regeocode.formatted_address // 这一条是详细位置\n return res[1].data.regeocode // 返回详细地址信息\n } else {\n throw new Error('无法获取详细地址')\n }\n } catch (err) {\n console.error('逆地理编码请求失败:', err)\n throw err // 抛出错误以便调用者处理\n }\n}\n\n// 通过后端服务器获取详细位置\nexport const getMapDetailAddress = async (longitude, latitude) => {\n try {\n const res = await httpGet('/map/detail', {\n longitude,\n latitude,\n })\n if (res.code == 200) {\n return res.data\n }\n return null\n } catch (err) {\n console.error('获取详细地址信息失败:', err)\n throw err\n }\n}\n\n// 获取当前经纬度信息\nexport const getLocation = async () => {\n const currentTime = Date.now()\n\n // 从本地缓存中获取上次的位置信息\n const cachedLocation = uni.getStorageSync(CACHE_KEY)\n\n // 如果有缓存并且未超过30秒,直接返回缓存的位置信息\n if (cachedLocation && currentTime - cachedLocation.time < THROTTLE_TIME) {\n return cachedLocation.data // 返回缓存中的数据\n }\n\n try {\n const res = await new Promise((resolve, reject) => {\n uni.getLocation({\n type: 'gcj02',\n success: (res) => {\n resolve(res) // 成功时返回结果\n },\n fail: (err) => {\n reject(err) // 失败时抛出错误\n },\n });\n });\n\n // 成功获取到位置信息后,缓存到本地存储\n uni.setStorageSync(CACHE_KEY, {\n data: res,\n time: currentTime, // 记录获取位置的时间戳\n })\n\n return res // 返回最新的位置信息\n } catch (err) {\n console.log(err)\n toast('获取位置失败')\n throw err // 抛出错误,供调用者处理\n }\n}\n"],"names":["httpGet","uni","res","toast"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAGA,MAAM,YAAY;AAClB,MAAM,gBAAgB;AAsBV,MAAC,sBAAsB,CAAO,WAAW,aAAa;AAChE,MAAI;AACF,UAAM,MAAM,MAAMA,WAAO,QAAC,eAAe;AAAA,MACvC;AAAA,MACA;AAAA,IACN,CAAK;AACD,QAAI,IAAI,QAAQ,KAAK;AACnB,aAAO,IAAI;AAAA,IACZ;AACD,WAAO;AAAA,EACR,SAAQ,KAAK;AACZ,YAAQ,MAAM,eAAe,GAAG;AAChC,UAAM;AAAA,EACP;AACH;AAGY,MAAC,cAAc,MAAY;AACrC,QAAM,cAAc,KAAK,IAAK;AAG9B,QAAM,iBAAiBC,cAAAA,MAAI,eAAe,SAAS;AAGnD,MAAI,kBAAkB,cAAc,eAAe,OAAO,eAAe;AACvE,WAAO,eAAe;AAAA,EACvB;AAED,MAAI;AACF,UAAM,MAAM,MAAM,IAAI,QAAQ,CAAC,SAAS,WAAW;AACjDA,oBAAAA,MAAI,YAAY;AAAA,QACd,MAAM;AAAA,QACN,SAAS,CAACC,SAAQ;AAChB,kBAAQA,IAAG;AAAA,QACZ;AAAA,QACD,MAAM,CAAC,QAAQ;AACb,iBAAO,GAAG;AAAA,QACX;AAAA,MACT,CAAO;AAAA,IACP,CAAK;AAGDD,kBAAG,MAAC,eAAe,WAAW;AAAA,MAC5B,MAAM;AAAA,MACN,MAAM;AAAA;AAAA,IACZ,CAAK;AAED,WAAO;AAAA,EACR,SAAQ,KAAK;AACZ,YAAQ,IAAI,GAAG;AACfE,oBAAAA,MAAM,QAAQ;AACd,UAAM;AAAA,EACP;AACH;;;"} |