1


have you done any live tracking demo like Uber in react native?

i made one. it's working fine in emulator & simulator with custom location but couldn't get proper location in real device using navigator.geolocation.

watchID = null;

componentDidMount(){


    navigator.geolocation.getCurrentPosition((position) => {
        let lat = parseFloat(position.coords.latitude);
        let long = parseFloat(position.coords.longitude);

        let initialRegion = {
            latitude : lat,
            longitude : long,
            latitudeDelta : LATITUDE_DELTA,
            longitudeDelta : LONGITUDE_DELTA
        }

        this.setState({ initialPosition : initialRegion });
        this.setState({ markerPostition : initialRegion });
    }, (error) => { 
        alert(JSON.stringify(error)) 
    });


    this.watchID = navigator.geolocation.watchPosition((position) => {

         let lat = parseFloat(position.coords.latitude);
         let long = parseFloat(position.coords.longitude);

        let lastRegion = {
            latitude : lat,
            longitude : long,
            latitudeDelta : LATITUDE_DELTA,
            longitudeDelta : LONGITUDE_DELTA
        }

        this.setState({ initialPosition : lastRegion });
        this.setState({ markerPostition : lastRegion });    
    });


}

componentWillUnmount(){
    navigator.geolocation.clearWatch(this.watchID);
}

i had set enableHighAccuracy to true but it was throwing error, so, i just removed it. now i'm not getting proper (exact) location of device.

please suggest me Geolocation setup with watchPosition what's worked better for you in case of you made any related project(s).

if you have your code in git repo then i will be thankful of your. forgive me if i did any grammatical mistakes.

3 Answers 3

1
componentDidMount() {
  Geolocation.getCurrentPosition(
    (pos) => {
      const coords = {
        latitude: pos.coords.latitude,
        longitude: pos.coords.longitude
      };
      this.setState((prevState) => {
        return {
          focusedLocation: {
            ...prevState.focusedLocation,
            latitude: coords.latitude,
            longitude: coords.longitude
          }
        };
      });
      console.log(coords.latitude, coords.longitude);
      return this.geocoder(coords.latitude, coords.longitude);
    },
    (err) => {
      alert('Fetching the Position failed, please check location is enable!');
    },
    { enableHighAccuracy: false, timeout: 10000 }
  );
}

this will work perfectly...try this

Sign up to request clarification or add additional context in comments.

1 Comment

Add code formatting to your code on first line and consider adding some explanation
0

I have used geolocation in my project and it works perfectly, here is the setup of my geolocation try if it fixes your issue

 navigator.geolocation.getCurrentPosition(
            (position) => {
              //position.coords.latitude for latitude 
              //position.coords.longitude for longitude
            },
            (error) => console.warn(error.message),
            { enableHighAccuracy: false, timeout: 10000 }
          )

Watchposition code is same as yours

1 Comment

thanks Shashank Srivastava, i solved my issue by add add {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000} in getCurrentPosition & by adding { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000, distanceFilter: 10 } in watchPosition thanks
0

enableHighAccuracy: true which will help to find the exact location

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.