react-native-geolocation icon indicating copy to clipboard operation
react-native-geolocation copied to clipboard

GeoLocation.watchPosition() is not watching real time

Open nimahkh opened this issue 4 years ago • 2 comments

Environment

System:
    OS: Linux 4.15 Ubuntu 18.04.1 LTS (Bionic Beaver)
    CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
    Memory: 269.52 MB / 11.65 GB
    Shell: 5.4.2 - /usr/bin/zsh
  Binaries:
    Node: 12.0.0 - ~/.nvm/versions/node/v12.0.0/bin/node
    Yarn: 1.16.0 - /usr/bin/yarn
    npm: 6.9.0 - ~/.nvm/versions/node/v12.0.0/bin/npm
    Watchman: Not Found
  SDKs:
    Android SDK: Not Found
  IDEs:
    Android Studio: Not Found
  Languages:
    Java: 1.8.0_252 - /usr/bin/javac
    Python: 2.7.17 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.11.0 => 16.11.0 
    react-native: 0.62.2 => 0.62.2 
  npmGlobalPackages:
    *react-native*: Not Found

Platforms

Android

Versions

  • Android:
  • iOS:
  • react-native-geolocation: 2.0.2
  • react-native: 16.11.0
  • react:0.62.2

Description

I want to listen to the location of user. so with this article, i started: https://medium.com/quick-code/react-native-location-tracking-14ab2c9e2db8

I decided to separate codes and now it is like this:

const watchID = React.useRef(false);
...
function start(){
 watchID.current = Geolocation.watchPosition(
      position => {
        const { latitude:latitude_value, longitude:longitude_value } = position.coords;

        const newCoordinate = {
          latitude,
          longitude
        };

        console.log('New Coordinate',routeCoordinates.concat([newCoordinate]));

        if (Platform.OS === "android") {
          if (marker.current) {
            marker.current.animateMarkerToCoordinate(
              newCoordinate,
              500
            );
          }
        } else {
          coordinate.timing(newCoordinate).start();
        }

        setLatitude(latitude_value)
        setLongitude(longitude_value)
        setRouteCoordinates(routeCoordinates.concat([newCoordinate]))
        setDistance(distanceTravelled + calcDistance(newCoordinate))
        setPrevLatLng(newCoordinate)
      },
      error => console.log(error),
      {
        enableHighAccuracy: true,
        timeout: 20000,
        maximumAge: 1000,
        distanceFilter: 10
      }
    );
}
...
  React.useEffect(()=>{
    return()=>{
      console.log('clear watch id');
      Geolocation.clearWatch(watchID.current);
    }
  },[])

...


<TouchableOpacity onPress={start} ... >
....
</TouchableOpacity>

with the code that i wrote above, I expect to see 'New Coordinate' log, when i am walking. but it does not happening. what I did wrong?

nimahkh avatar May 22 '20 12:05 nimahkh

I got it solve decreasing the distanceFilter parameter. How you want to get new location walking I advise set distanceFilter to 1 for example.

Remember that distanceFilter is in meters (m)

watchPosition Documentation

jefersonvinicius avatar May 29 '20 02:05 jefersonvinicius

yeah but doesnt that cause much bigger battery drain?

dakkafex avatar Aug 29 '20 23:08 dakkafex