2018-06-27 11:17:38 +02:00
|
|
|
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 = (
|
2018-08-05 15:47:28 +02:00
|
|
|
(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;
|
|
|
|
|
2018-08-13 22:07:34 +02:00
|
|
|
let heading = null;
|
|
|
|
if (Math.random() > 0.25) {
|
|
|
|
heading = Math.random() * 360;
|
|
|
|
}
|
2018-07-04 17:37:28 +02:00
|
|
|
const newLocation = {
|
2018-06-26 11:04:23 +02:00
|
|
|
coords: {
|
2018-06-27 11:17:38 +02:00
|
|
|
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,
|
2018-08-13 22:07:34 +02:00
|
|
|
heading,
|
2018-06-26 11:04:23 +02:00
|
|
|
},
|
2018-08-02 16:58:08 +02:00
|
|
|
timestamp: new Date().getTime(),
|
2018-06-26 11:04:23 +02:00
|
|
|
};
|
2018-07-04 17:37:28 +02:00
|
|
|
console.log('New mock location: ', newLocation);
|
|
|
|
return newLocation;
|
2018-06-26 11:04:23 +02:00
|
|
|
}
|
2018-06-28 14:40:56 +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
|
2018-08-05 15:47:28 +02:00
|
|
|
e.code === 22
|
2018-06-28 14:40:56 +02:00
|
|
|
// Firefox
|
2018-08-05 15:47:28 +02:00
|
|
|
|| e.code === 1014
|
2018-06-28 14:40:56 +02:00
|
|
|
// test name field too, because code might not be present
|
|
|
|
// everything except Firefox
|
2018-08-05 15:47:28 +02:00
|
|
|
|| e.name === 'QuotaExceededError'
|
2018-06-28 14:40:56 +02:00
|
|
|
// Firefox
|
2018-08-05 15:47:28 +02:00
|
|
|
|| e.name === 'NS_ERROR_DOM_QUOTA_REACHED'
|
|
|
|
)
|
|
|
|
// acknowledge QuotaExceededError only if there's something already stored
|
|
|
|
&& storage.length !== 0;
|
2018-06-28 14:40:56 +02:00
|
|
|
}
|
|
|
|
}
|