use fine location with no maximum age

This commit is contained in:
Bart Akeley 2018-12-22 10:27:27 -06:00
parent ad444cf90b
commit 6822f8af61

View file

@ -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);