1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/ReactNativeClient/lib/geolocation-node.js

26 lines
705 B
JavaScript
Raw Normal View History

const { shim } = require('lib/shim.js');
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 };