oak-frontend-base/lib/features/location.js

107 lines
5.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Location = void 0;
var tslib_1 = require("tslib");
var Feature_1 = require("../types/Feature");
var Location = /** @class */ (function (_super) {
tslib_1.__extends(Location, _super);
function Location() {
return _super !== null && _super.apply(this, arguments) || this;
}
Location.prototype.get = function (acceptableLatency) {
if (acceptableLatency === void 0) { acceptableLatency = 10000; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var result, getGeolocation, result;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this.lastTimestamp &&
Date.now() - this.lastTimestamp < acceptableLatency) {
return [2 /*return*/, {
latitude: this.latitude,
longitude: this.longitude,
}];
}
if (!(process.env.OAK_PLATFORM === 'wechatMp')) return [3 /*break*/, 2];
return [4 /*yield*/, wx.getLocation({
type: 'gcj02',
})];
case 1:
result = _a.sent();
this.latitude = result.latitude;
this.longitude = result.longitude;
this.lastTimestamp = Date.now();
return [2 /*return*/, {
latitude: this.latitude,
longitude: this.longitude,
}];
case 2:
getGeolocation = function () {
return new Promise(function (resolve, reject) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
// 1. coords.latitude估计纬度
// 2. coords.longitude估计经度
// 3. coords.altitude估计高度
// 4. coords.accuracy所提供的以米为单位的经度和纬度估计的精确度
// 5. coords.altitudeAccuracy所提供的以米为单位的高度估计的精确度
// 6. coords.heading 宿主设备当前移动的角度方向,相对于正北方向顺时针计算
// 7. coords.speed以米每秒为单位的设备的当前对地速度
var coords = position.coords;
var location = {
accuracy: coords.accuracy,
altitude: coords.altitude,
altitudeAccuracy: coords.altitudeAccuracy,
heading: coords.heading,
latitude: coords.latitude,
longitude: coords.longitude,
speed: coords.speed,
};
resolve(location);
}, function (error) {
reject(error);
}, {
// 指示浏览器获取高精度的位置默认为false
enableHighAccuracy: true,
// 指定获取地理位置的超时时间,默认不限时,单位为毫秒
timeout: 10000,
// 最长有效期,在重复获取地理位置时,此参数指定多久再次获取位置。
maximumAge: 3000,
});
}
else {
reject('Your browser does not support Geolocation!');
}
});
};
return [4 /*yield*/, getGeolocation()];
case 3:
result = (_a.sent());
this.lastTimestamp = Date.now();
this.latitude = result.latitude;
this.longitude = result.longitude;
return [2 /*return*/, {
latitude: this.latitude,
longitude: this.longitude,
}];
}
});
});
};
Location.prototype.refresh = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.get()];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
tslib_1.__decorate([
Feature_1.Action
], Location.prototype, "refresh", null);
return Location;
}(Feature_1.Feature));
exports.Location = Location;