Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_startWatchingLocation = async () => {
const { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
return;
}
const subscription = await Location.watchPositionAsync(
{
accuracy: Location.Accuracy.High,
timeInterval: 1000,
distanceInterval: 1,
},
location => {
// tslint:disable-next-line no-console
console.log(`Got location: ${JSON.stringify(location.coords)}`);
this.setState({ watchLocation: location });
}
);
this.setState({ subscription });
}
startWatching = async () => {
let { status } = await Permissions.askAsync(Permissions.LOCATION)
if (status !== 'granted') {
// TODO: show error message
}
await Location.watchPositionAsync({
accuracy: Location.Accuracy.BestForNavigation,
timeInterval: 1000,
distanceInterval: 5
}, location => {
console.log('location', location)
})
}
onAccuracyChange = () => {
const next = Location.Accuracy[this.state.accuracy + 1];
const accuracy = next ? Location.Accuracy[next] : Location.Accuracy.Lowest;
this.setState({ accuracy });
if (this.state.isTracking) {
// Restart background task with the new accuracy.
this.startLocationUpdates(accuracy);
}
};
type AppStateType = "active" | "background" | "inactive";
type Props = {
children: React.Node,
permissions: PermissionsType
};
const defaultContext: LocationContextType = {
error: false
};
const positionOptions = {
// See https://docs.expo.io/versions/v32.0.0/sdk/location/#locationaccuracy
// this is the best possible accuracy using GPS and other sensors
accuracy: Location.Accuracy.BestForNavigation,
// This is the interval between location updates. We should get a new GPS
// reading every 2000ms.
timeInterval: 2000
};
// Timeout between location updates --> means location was probably turned off
// so we need to check it.
const LOCATION_TIMEOUT = 10000;
const LocationContext = React.createContext(
defaultContext
);
/**
* The LocationProvider provides details about the current device location based
* on sensors including GPS. It must be included in the component heirarchy
async function startTrace (dispatch) {
dispatch({
type: types.TRACE_START
})
let { status } = await Permissions.askAsync(Permissions.LOCATION)
if (status !== 'granted') {
// TODO: show error message
}
const watcher = await Location.watchPositionAsync({
accuracy: Location.Accuracy.BestForNavigation,
timeInterval: 1000, // TODO: get from config
distanceInterval: 5 // TODO: get from config
}, location => {
dispatch({
type: types.TRACE_POINT_CAPTURED,
location
})
})
dispatch({
type: types.TRACE_SET_SUBSCRIPTION,
watcher
})
}
import * as TaskManager from 'expo-task-manager';
import Button from '../../components/Button';
import Colors from '../../constants/Colors';
const STORAGE_KEY = 'expo-home-locations';
const LOCATION_UPDATES_TASK = 'location-updates';
const locationEventsEmitter = new EventEmitter();
const locationAccuracyStates: { [key in Location.Accuracy]: Location.Accuracy } = {
[Location.Accuracy.Lowest]: Location.Accuracy.Low,
[Location.Accuracy.Low]: Location.Accuracy.Balanced,
[Location.Accuracy.Balanced]: Location.Accuracy.High,
[Location.Accuracy.High]: Location.Accuracy.Highest,
[Location.Accuracy.Highest]: Location.Accuracy.BestForNavigation,
[Location.Accuracy.BestForNavigation]: Location.Accuracy.Lowest,
};
const locationActivityTypes: { [key in Location.ActivityType]: Location.ActivityType | undefined } = {
[Location.ActivityType.Other]: Location.ActivityType.AutomotiveNavigation,
[Location.ActivityType.AutomotiveNavigation]: Location.ActivityType.Fitness,
[Location.ActivityType.Fitness]: Location.ActivityType.OtherNavigation,
[Location.ActivityType.OtherNavigation]: Location.ActivityType.Airborne,
[Location.ActivityType.Airborne]: undefined,
};
interface Props {
navigation: NavigationScreenProp<{}, any>;
}
interface State {
showsUserLocation>
{this.renderPolyline()}
import MapView from 'react-native-maps';
import * as Location from 'expo-location';
import * as Permissions from 'expo-permissions';
import * as TaskManager from 'expo-task-manager';
import Button from '../../components/Button';
import Colors from '../../constants/Colors';
const STORAGE_KEY = 'expo-home-locations';
const LOCATION_UPDATES_TASK = 'location-updates';
const locationEventsEmitter = new EventEmitter();
const locationAccuracyStates: { [key in Location.Accuracy]: Location.Accuracy } = {
[Location.Accuracy.Lowest]: Location.Accuracy.Low,
[Location.Accuracy.Low]: Location.Accuracy.Balanced,
[Location.Accuracy.Balanced]: Location.Accuracy.High,
[Location.Accuracy.High]: Location.Accuracy.Highest,
[Location.Accuracy.Highest]: Location.Accuracy.BestForNavigation,
[Location.Accuracy.BestForNavigation]: Location.Accuracy.Lowest,
};
const locationActivityTypes: { [key in Location.ActivityType]: Location.ActivityType | undefined } = {
[Location.ActivityType.Other]: Location.ActivityType.AutomotiveNavigation,
[Location.ActivityType.AutomotiveNavigation]: Location.ActivityType.Fitness,
[Location.ActivityType.Fitness]: Location.ActivityType.OtherNavigation,
[Location.ActivityType.OtherNavigation]: Location.ActivityType.Airborne,
[Location.ActivityType.Airborne]: undefined,
};
interface Props {
const STORAGE_KEY = 'expo-home-locations';
const LOCATION_UPDATES_TASK = 'location-updates';
const locationEventsEmitter = new EventEmitter();
export default class LocationDiagnosticsScreen extends React.Component {
static navigationOptions = {
title: 'Location Diagnostics',
};
mapViewRef = React.createRef();
state = {
isBackgroundLocationAvailable: null,
accuracy: Location.Accuracy.High,
isTracking: false,
showsBackgroundLocationIndicator: false,
savedLocations: [],
initialRegion: null,
error: null,
};
componentDidMount() {
this.checkBackgroundLocationAvailability();
}
async checkBackgroundLocationAvailability() {
const isBackgroundLocationAvailable = await Location.isBackgroundLocationAvailableAsync();
this.setState({ isBackgroundLocationAvailable });
}