UNPKG

minigame-std

Version:

Cross-platform standard library for WeChat minigame and web browsers with unified APIs for crypto, fs, fetch, storage, and more.

72 lines (71 loc) 1.99 kB
import { Err, Ok } from "happy-rusty"; import { asyncIOResultify } from "./utils.mjs"; import { Future } from "tiny-future"; //#region src/macros/env.ts /** * 如果在小游戏环境中返回 true,否则返回 false。 */ const IS_MINA = __MINIGAME_STD_MINA__; //#endregion //#region src/std/lbs/mina_lbs.ts /** * 获取当前 geo 坐标。 * @returns 异步的经纬度结果。 */ async function getCurrentPosition$2() { const hasFuzzy = typeof wx.getFuzzyLocation === "function"; const getLocation = hasFuzzy ? wx.getFuzzyLocation : wx.getLocation; const scope = hasFuzzy ? "scope.userFuzzyLocation" : "scope.userLocation"; const authRes = await asyncIOResultify(wx.authorize)({ scope }); if (authRes.isErr()) return authRes.asErr(); return (await asyncIOResultify(getLocation)({ type: "wgs84" })).map((pos) => ({ latitude: pos.latitude, longitude: pos.longitude })); } //#endregion //#region src/std/lbs/web_lbs.ts /** * @internal * Web 平台的地理位置服务实现。 */ /** * 获取当前 geo 坐标。 * @returns 异步的经纬度结果。 */ function getCurrentPosition$1() { const future = new Future(); navigator.geolocation.getCurrentPosition((position) => { future.resolve(Ok({ latitude: position.coords.latitude, longitude: position.coords.longitude })); }, (err) => { future.resolve(Err(new Error(err.message))); }); return future.promise; } //#endregion //#region src/std/lbs/mod.ts /** * 获取当前 geo 坐标。 * @returns 当前经纬度。 * @since 1.7.0 * @example * ```ts * const result = await getCurrentPosition(); * if (result.isOk()) { * const pos = result.unwrap(); * console.log('纬度:', pos.latitude); * console.log('经度:', pos.longitude); * } else { * console.error('获取位置失败:', result.unwrapErr()); * } * ``` */ function getCurrentPosition() { return IS_MINA ? getCurrentPosition$2() : getCurrentPosition$1(); } //#endregion export { getCurrentPosition }; //# sourceMappingURL=lbs.mjs.map