1

I'm using Geolocation in React-Native to receive my current location to load object around me. Below is my code.

getCurrentPosition() {
    console.log("checkLocation", "getCurrentPosition1");
    navigator.geolocation.getCurrentPosition(
      position => {
        const { coords } = position;
        if (coords !== undefined) {
          console.log("checkLocation", "getCurrentPosition trigger");
          this.setState({
            currentCoordinate: coords,
            prevCoorForGet: coords,
            prevCoorForUpdate: coords
          });
          this.props.saveCurrentLocation({
            currentLocation: {
              latitude: coords.latitude,
              longitude: coords.longitude
            }
          });
          this.loadContent(coords);
        }
      },
      error =>
        console.log(
          "checkLocation",
          "getCurrentPosition " + JSON.stringify(error)
        ),
      {
        enableHighAccuracy: true,
        timeout: 60000,
        maximumAge: 3600000
      }
    ); 
 }

The problem is this code working fine in the first time. But when I navigate to another scene and go back, It isn't work anymore and give me a time out error. Sometime it works, sometime it doesn't. Please help me fix it.

ToraCode
  • 421
  • 8
  • 19

3 Answers3

0

This one is working fine (both ios and android) in my project even working after navigating between pages and return map screen.

navigator.geolocation.getCurrentPosition(
        (position) => {
            // user location's latitude and longitude
            let latitude = parseFloat(position.coords.latitude);
            let longitude = parseFloat(position.coords.longitude);

            console.log('location position: ', position);

            let region = {
                latitude: latitude,
                longitude: longitude,
                latitudeDelta: 0.0522,
                longitudeDelta: 0.0321,
            };

            // to store region data
            this.setState({region: region});

            //animate to user current location
            this.map.animateToRegion(region,1000)


        },
        (error) => console.log('position error!!!', error),
        {enableHighAccuracy: false, timeout: 3000}
    );

I hope it works on your project too

Edit

If still not working

    /**
     * when return the page this listener will trigger
     */
    this.props.navigation.addListener('willFocus', (payload) => {

        // call location method again
        this.getLocation()

    });
Orhan
  • 169
  • 1
  • 7
  • Tks for comment, the timeout error is very random. Sometime it work like a charm, but sometime it take a long time to get. I guest that may be because `enableHighAccuracy` is set to `true`. `False` is working but I afraid of this. – ToraCode Jun 21 '19 at 11:02
0

Try below code, this seems to work for me

import Geolocation from 'react-native-geolocation-service';

componentWillUnmount() {
    Geolocation.stopObserving();
}
Ashish Singh Rawat
  • 1,419
  • 16
  • 30
0

use your function in componentWillMount() so that every time the component mounts....it function will be executed