1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-09 08:45:55 +02:00
joplin/packages/lib/geolocation-node.js

26 lines
704 B
JavaScript
Raw Normal View History

2020-11-05 18:58:23 +02:00
const shim = require('./shim').default;
2017-07-10 20:09:58 +02:00
class GeolocationNode {
static async currentPosition(options = null) {
if (!options) options = {};
let response = await shim.fetch('https://freegeoip.app/json/');
2019-09-19 23:51:18 +02:00
if (!response.ok) throw new Error(`Could not get geolocation: ${await response.text()}`);
2017-07-10 20:09:58 +02:00
response = await response.json();
if (!('latitude' in response) || !('longitude' in response)) throw new Error(`Invalid geolocation response: ${response ? JSON.stringify(response) : '<null>'}`);
2017-07-10 20:09:58 +02:00
return {
2019-07-29 15:43:53 +02:00
timestamp: new Date().getTime(),
2017-07-10 20:09:58 +02:00
coords: {
longitude: response.longitude,
2017-07-10 20:09:58 +02:00
altitude: 0,
latitude: response.latitude,
2019-07-29 15:43:53 +02:00
},
};
2017-07-10 20:09:58 +02:00
}
}
2019-07-29 15:43:53 +02:00
module.exports = { GeolocationNode };