cygnal/src/tools/index.js

71 lines
2.0 KiB
JavaScript
Raw Normal View History

import { EARTH_RADIUS } from '@/constants';
2018-06-26 11:04:23 +02:00
export function distance(latLng1, latLng2) {
const lat1 = (latLng1[0] * Math.PI) / 180;
const lng1 = (latLng1[1] * Math.PI) / 180;
const lat2 = (latLng2[0] * Math.PI) / 180;
const lng2 = (latLng2[1] * Math.PI) / 180;
const a = (
(Math.sin((lat2 - lat1) / 2.0) ** 2)
+ (Math.cos(lat1) * Math.cos(lat2) * (Math.sin((lng2 - lng1) / 2.0) ** 2))
2018-06-26 11:04:23 +02:00
);
const c = 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return EARTH_RADIUS * c;
}
export function mockLocation() {
2018-07-12 17:48:26 +02:00
// Over Paris
/*
2018-06-26 11:04:23 +02:00
const LAT_MIN = 48.854031;
const LNG_MIN = 2.281279;
const LAT_MAX = 48.886123;
const LNG_MAX = 2.392742;
2018-07-12 17:48:26 +02:00
*/
// Over small area
const LAT_MIN = 48.81788;
const LNG_MIN = 2.31723;
const LAT_MAX = 48.81952;
const LNG_MAX = 2.32077;
const newLocation = {
2018-06-26 11:04:23 +02:00
coords: {
accuracy: 10, // In meters
2018-06-26 11:04:23 +02:00
latitude: (Math.random() * (LAT_MAX - LAT_MIN)) + LAT_MIN,
longitude: (Math.random() * (LNG_MAX - LNG_MIN)) + LNG_MIN,
heading: null, // 20 * (Math.PI / 180),
2018-06-26 11:04:23 +02:00
},
timestamp: new Date().getTime(),
2018-06-26 11:04:23 +02:00
};
console.log('New mock location: ', newLocation);
return newLocation;
2018-06-26 11:04:23 +02:00
}
export function storageAvailable(type) {
let storage;
try {
storage = window[type];
const x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
} catch (e) {
return e instanceof DOMException && (
// everything except Firefox
e.code === 22
// Firefox
|| e.code === 1014
// test name field too, because code might not be present
// everything except Firefox
|| e.name === 'QuotaExceededError'
// Firefox
|| e.name === 'NS_ERROR_DOM_QUOTA_REACHED'
)
// acknowledge QuotaExceededError only if there's something already stored
&& storage.length !== 0;
}
}