1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-15 09:04:04 +02:00
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 20:09:58 +02:00
class GeolocationNode {
static async currentPosition(options = null) {
if (!options) options = {};
const ip = await netUtils.ip();
2019-09-19 23:51:18 +02: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 20:09:58 +02:00
response = await response.json();
2019-09-19 23:51:18 +02:00
if (!('lat' in response) || !('lon' 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.lon,
2017-07-10 20:09:58 +02:00
altitude: 0,
2019-07-29 15:43:53 +02:00
latitude: response.lat,
},
};
2017-07-10 20:09:58 +02:00
}
}
2019-07-29 15:43:53 +02:00
module.exports = { GeolocationNode };