miniapp-web-jsapi
Version:
JSAPI/View adapter for miniprogram running on the web
35 lines • 1.85 kB
JavaScript
import { asyncRun } from "../../../common";
export function getLocation(options) {
var notifyFailed = function notifyFailed(errorCode) {
asyncRun(function () {
var _options$fail, _options$complete;
var result = {
error: errorCode
};
(_options$fail = options.fail) === null || _options$fail === void 0 ? void 0 : _options$fail.call(options, result);
(_options$complete = options.complete) === null || _options$complete === void 0 ? void 0 : _options$complete.call(options, result);
});
};
var geo = navigator.geolocation;
if (!geo) {
notifyFailed(13);
return;
}
geo.getCurrentPosition(function (position) {
var _position$coords, _position$coords2, _position$coords3, _position$coords4, _options$success, _options$complete2;
var result = {
accuracy: "".concat((_position$coords = position.coords) === null || _position$coords === void 0 ? void 0 : _position$coords.accuracy),
horizontalAccuracy: "".concat((_position$coords2 = position.coords) === null || _position$coords2 === void 0 ? void 0 : _position$coords2.altitudeAccuracy),
latitude: "".concat((_position$coords3 = position.coords) === null || _position$coords3 === void 0 ? void 0 : _position$coords3.latitude),
longitude: "".concat((_position$coords4 = position.coords) === null || _position$coords4 === void 0 ? void 0 : _position$coords4.longitude)
};
(_options$success = options.success) === null || _options$success === void 0 ? void 0 : _options$success.call(options, result);
(_options$complete2 = options.complete) === null || _options$complete2 === void 0 ? void 0 : _options$complete2.call(options, result);
}, function (err) {
var error = err.code === 1 ? 11 : 13;
notifyFailed(error);
}, {
timeout: 10000,
maximumAge: options.cacheTimeout || 30000
});
}