Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- componentDidMount() {
- this.getCurrentLocation(); // Get users current location
- }
- componentDidUpdate(prevProps, prevState) {
- // If prevState changes
- if (prevState.newLatitude !== this.state.newLatitude) {
- // calculate the distance between initial and new coordinates
- this.getDistance();
- }
- }
- componentWillUnmount() {
- // Remember to clear all listeners
- navigator.geolocation.clearWatch(this.watchUserUserPosition());
- }
- getLocation() {
- navigator.geolocation.getCurrentPosition((position) => {
- this.setState({
- latitude: position.coords.latitude,
- longitude: position.coords.longitude
- }, () => {
- this.sendToFirebase();
- this.watchUserPosition(); //continue listening for location changes
- });
- },
- (error) => {
- //Handle error
- },
- { enableHighAccuracy: false, timeout: 200000, maximumAge: 1000 },
- );
- }
- watchUserPosition() {
- navigator.geolocation.watchPosition(
- (position) => {
- this.setState({
- newLatitude: position.coords.latitude,
- newLongitude: position.coords.longitude,
- error: null,
- });
- },
- (error) => this.setState({ error: error.message }),
- { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000, distanceFilter: 10 },
- );
- }
- getDistance() {
- let initial = {latitude: this.state.latitude, longitude: this.state.longitude};
- let newCoord = {latitude: this.state.newLatitude, longitude: this.state.newLongitude};
- const distance = geolib.getDistance(initial, newCoord);
- if (distance >== 100) {
- Alert.alert("Success!", "You have reached 100meters!");
- this.sendToFirebase(); // whatever you are saving to firebase
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement