t-comm
Version:
专业、稳定、纯粹的工具库
55 lines (52 loc) • 1.87 kB
JavaScript
import { getGameletPixuiFrame } from './gamelet.mjs';
// 定位 flag,标记定位返回结果的类型
var LocationFlagInPixui;
(function (LocationFlagInPixui) {
LocationFlagInPixui[LocationFlagInPixui["LocationSuccess"] = 1] = "LocationSuccess";
LocationFlagInPixui[LocationFlagInPixui["LocationNoPermission"] = 2] = "LocationNoPermission";
LocationFlagInPixui[LocationFlagInPixui["LocationFailed"] = 3] = "LocationFailed";
})(LocationFlagInPixui || (LocationFlagInPixui = {}));
/**
* 获取游戏定位信息,回调数据
* {
* flag: LocationFlagInPixui,
* lat: number,
* lng: number,
* }
*/
var getLocationInPixui = function getLocationInPixui(platformAPI) {
return new Promise(function (resolve) {
var _onGameCommandListener = function onGameCommandListener(message) {
try {
var data = JSON.parse(message);
if (data.type === 'updateLocation') {
if (data.longitude == 0 && data.latitude == 0) {
// 游戏返回的经纬度值均为 0,则表示用户 GPS 系统开关未打开
resolve({
flag: LocationFlagInPixui.LocationNoPermission,
lat: 0,
lng: 0
});
} else {
resolve({
flag: LocationFlagInPixui.LocationSuccess,
lat: +data.latitude,
lng: +data.longitude
});
}
// 去除监听
getGameletPixuiFrame().GameletAPI.removeOnGameCommandListener(_onGameCommandListener);
}
} catch (e) {
console.error(e);
}
};
// 监听游戏回调地图信息
getGameletPixuiFrame().GameletAPI.addOnGameCommandListener(_onGameCommandListener);
platformAPI.callGame(JSON.stringify({
type: 'checkLocation',
content: '1'
}));
});
};
export { LocationFlagInPixui, getLocationInPixui };