react native get location accuaracy problem

64 Views Asked by At

i am building one cycling react native app that tracks user cycled data like speed,distance travlled, calories burnt data. i am using react-native-get-location npm library to get the current location of the user using get current position method.to calcluate distance travelled i am using harvesine problem.

my logic to calculated distance travelled is i calling get current position method every 5 second i am storing previous and current cordinates and finding distance between two coordinates using harvesine formula each time.

my problem is location library returns user location cordinates between 10m around range each time every 5 seconds .thus distance tranvelled increases even if the user not moves each time.

(i am using react-native mapbox it is not having any distance travelled ,speed funcationality features api like google maps i am calculating each data like speed,avg speed ,distance travelled manually)

get position

 Geolocation.getCurrentPosition(
            (res) => {
                console.log("SOLO RIDE get position location called")
                setLocationEnabled(true);
                console.log(res);
                //getting user location
                console.log("lattitude", res.coords.latitude);
                console.log("longitude", res.coords.longitude);
                setUserCordinates([res.coords.longitude, res.coords.latitude]);
                fetchRoute([res.coords.longitude, res.coords.latitude]);
                getDistances([res.coords.longitude, res.coords.latitude]);

            },
            (error) => {
                console.log("get location error", error);
                console.log("please enable location ")
                setLocationEnabled(false);
            },
            { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000,accuracy:{
                android:'high',
                ios:'bestForNavigation',
              } }

        );

calculate distance

const getDistances = async (newcords) => {

        const start = {
            latitude: prevCords.current[1],
            longitude: prevCords.current[0]
        }

        const end = {
            latitude: newcords[1],
            longitude: newcords[0]
        }
        // console.log('prev cords', prevCords.current);
        // console.log('new cords', newcords);

        let d = haversine(start, end, { unit: 'meter' });
       
        
        let totaldistance = distanceTravelled.current + d / 1000;
        console.log("distance b/w 2 cordinates ", d.toFixed(2) + " m");
        console.log("prev distance", distanceTravelled.current.toFixed(2));
        console.log("total distance traveled", totaldistance.toFixed(2));
        //setDistanceTravelled(totaldistance);

        distanceTravelled.current = totaldistance;
        prevCords.current = newcords;

}
0

There are 0 best solutions below