mirror of
https://gitlab.com/wheres-the-tp/ui-mobile.git
synced 2026-01-25 05:54:56 -06:00
use fine location with no maximum age
This commit is contained in:
parent
ad444cf90b
commit
6822f8af61
1 changed files with 38 additions and 15 deletions
|
|
@ -1,28 +1,51 @@
|
|||
// @flow
|
||||
import { emitter } from '../streams/LocationStream';
|
||||
import { getCoordsFromZip } from './GoogleMapsApi';
|
||||
import { AsyncStorage } from 'react-native';
|
||||
import { AsyncStorage, PermissionsAndroid, Platform } from 'react-native';
|
||||
|
||||
const {
|
||||
PERMISSIONS: { ACCESS_FINE_LOCATION },
|
||||
RESULTS,
|
||||
request: requestPermission,
|
||||
} = PermissionsAndroid;
|
||||
|
||||
let locationPermissionResponse = false;
|
||||
|
||||
const askLocationPermission = async () => {
|
||||
if (Platform.OS === 'ios') {
|
||||
locationPermissionResponse = RESULTS.GRANTED;
|
||||
} else if (locationPermissionResponse !== RESULTS.GRANTED) {
|
||||
locationPermissionResponse = await requestPermission(ACCESS_FINE_LOCATION, {
|
||||
title: 'Aretherecookies? Location Permission',
|
||||
message: 'Aretherecookies? would like to use your location to find some cookies. Is that OK?',
|
||||
});
|
||||
}
|
||||
return locationPermissionResponse === RESULTS.GRANTED;
|
||||
};
|
||||
|
||||
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,
|
||||
askLocationPermission().then(granted => {
|
||||
if (!granted) {
|
||||
return reject('Permission was not given for location');
|
||||
}
|
||||
);
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
(pos: Position) => {
|
||||
emitter(pos), resolve(pos);
|
||||
return pos;
|
||||
},
|
||||
err => {
|
||||
reject(err);
|
||||
},
|
||||
{
|
||||
enableHighAccuracy: true,
|
||||
timeout: 5000,
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// TODO actually implement geolocation for zipcode into lat/lng
|
||||
export const getPositionFromZip = async (zip: string) => {
|
||||
const { lat: latitude, lng: longitude } = await getCoordsFromZip(zip);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue