mirror of
https://gitlab.com/wheres-the-tp/ui-mobile.git
synced 2026-01-25 09:54:55 -06:00
53 lines
1.1 KiB
JavaScript
53 lines
1.1 KiB
JavaScript
// @flow
|
|
import { emitter } from '../streams/LocationStream';
|
|
import { getCoordsFromZip } from './GoogleMapsApi';
|
|
import { AsyncStorage } from 'react-native';
|
|
|
|
export const getCurrentPosition = () => {
|
|
return new Promise((resolve, reject) => {
|
|
navigator.geolocation.getCurrentPosition(
|
|
(pos: Position) => {
|
|
emitter(pos), resolve(pos);
|
|
return pos;
|
|
},
|
|
err => {
|
|
reject(err);
|
|
},
|
|
{
|
|
enableHighAccuracy: true,
|
|
timeout: 2000,
|
|
maximumAge: 1000,
|
|
}
|
|
);
|
|
});
|
|
};
|
|
|
|
// TODO actually implement geolocation for zipcode into lat/lng
|
|
export const getPositionFromZip = async (zip: string) => {
|
|
const { lat: latitude, lng: longitude } = await getCoordsFromZip(zip);
|
|
|
|
const pos: any = {
|
|
coords: {
|
|
latitude,
|
|
longitude,
|
|
},
|
|
};
|
|
|
|
emitter(pos);
|
|
|
|
return pos;
|
|
};
|
|
|
|
export const getLocation: () => Promise<Position> = async () => {
|
|
const currentZip = await AsyncStorage.getItem('zipcode');
|
|
|
|
if (currentZip && currentZip !== 'usegps') {
|
|
return getPositionFromZip(currentZip);
|
|
} else {
|
|
AsyncStorage.setItem('zipcode', 'usegps');
|
|
return getCurrentPosition().catch(error => {
|
|
AsyncStorage.removeItem('zipcode');
|
|
throw error;
|
|
});
|
|
}
|
|
};
|