Skip to content

Instantly share code, notes, and snippets.

@ceessay
Last active February 9, 2019 17:38
Show Gist options
  • Save ceessay/3df51c15a36d634e08ff3bd95055e171 to your computer and use it in GitHub Desktop.
Save ceessay/3df51c15a36d634e08ff3bd95055e171 to your computer and use it in GitHub Desktop.
Expo gists
import React from "react";
import { StyleSheet, Text, View, Button } from "react-native";
import { Location, Permissions, TaskManager } from "expo";
const MY_LOCATION_TASK = "MY_LOCATION_TASK";
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
positions: []
};
}
async startTracking() {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== "granted") {
this.setState({
errorMessage: "Permission to access location was denied"
});
}
const options = {
accuracy: Location.Accuracy.High,
timeInterval: 1000 * 5,
distanceInterval: 5,
showsBackgroundLocationIndicator: true
};
Location.startLocationUpdatesAsync(MY_LOCATION_TASK, options).then(
locations => {
// console.debug("locations", JSON.stringify(locations));
},
error => {
alert("startLocationUpdatesAsync failed");
console.error("locations", error);
}
);
}
render() {
return (
<View style={styles.container}>
<Button title="Track Me" onPress={() => this.startTracking()} />
</View>
);
}
}
TaskManager.defineTask(MY_LOCATION_TASK, ({ data: { locations }, error }) => {
if (error) {
// check `error.message` for more details.
console.error("Location task error", error);
return;
}
//console.log("Received new locations", JSON.stringify(locations));
let { coords } = locations[0];
console.log("location detected", coords.longitude + " " + coords.longitude);
//send data to firebase
});
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center"
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment