react-nativemobileexpogeolocationlocation

Get Speed of Vehicle using Expo Location in React native


I'm doing the mobile application in Expo React Native that can track the vehicle speed. Currently I am using Expo Location API to track the vehicle location for every 5 seconds.

import * as Location from 'expo-location';

  const startLocationTracking = async () => {
  await Location.startLocationUpdatesAsync(LOCATION_TRACKING, {
      accuracy: Location.Accuracy.Highest,
      timeInterval: 5000,
      distanceInterval: 0,
  });
  const hasStarted = await Location.hasStartedLocationUpdatesAsync(
      LOCATION_TRACKING
  );
  setLocationStarted(hasStarted);
  console.log('tracking started?', hasStarted);
};

TaskManager.defineTask(LOCATION_TRACKING, async ({ data, error }) => {
 if (error) {
  console.log('LOCATION_TRACKING task ERROR:', error);
  return;
 }
 if (data) {
  Location.g
  const { locations } = data;
  let lat = locations[0].coords.latitude;
  let long = locations[0].coords.longitude;

  l1 = lat;
  l2 = long;
  origin = lat+','+long;
 
 }
 });

In Expo Location Document Document there is a option to get speed. image

Can anyone let me know how to track speed using this option. Or any other alternatives to track speed using coordinate movement change.


Solution

  • In Expo v51 it is possible to access a speed property from the LocationObjectCoords:

    Expo documentation