joplin/ReactNativeClient/lib/geolocation-node.js

29 lines
769 B
JavaScript
Raw Normal View History

const { shim } = require('lib/shim.js');
const { netUtils } = require('lib/net-utils.js');
2017-07-10 18:09:58 +00:00
class GeolocationNode {
static async currentPosition(options = null) {
if (!options) options = {};
const ip = await netUtils.ip();
2019-09-19 21:51:18 +00:00
let response = await shim.fetch(`http://ip-api.com/json/${ip}`);
if (!response.ok) throw new Error(`Could not get geolocation: ${await response.text()}`);
2017-07-10 18:09:58 +00:00
response = await response.json();
2019-09-19 21:51:18 +00:00
if (!('lat' in response) || !('lon' in response)) throw new Error(`Invalid geolocation response: ${response ? JSON.stringify(response) : '<null>'}`);
2017-07-10 18:09:58 +00:00
return {
2019-07-29 13:43:53 +00:00
timestamp: new Date().getTime(),
2017-07-10 18:09:58 +00:00
coords: {
longitude: response.lon,
2017-07-10 18:09:58 +00:00
altitude: 0,
2019-07-29 13:43:53 +00:00
latitude: response.lat,
},
};
2017-07-10 18:09:58 +00:00
}
}
2019-07-29 13:43:53 +00:00
module.exports = { GeolocationNode };