Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

expo location background example

import * as Location from 'expo-location';
import * as TaskManager from 'expo-task-manager';
import axios from 'axios';

const TASK_FETCH_LOCATION = 'TASK_FETCH_LOCATION';

// 1 define the task passing its name and a callback that will be called whenever the location changes
TaskManager.defineTask(TASK_FETCH_LOCATION, async ({ data: { locations }, error }) => {
  if (error) {
    console.error(error);
    return;
  }
  const [location] = locations;
  try {
    const url = `https://<your-api-endpoint>`;
    await axios.post(url, { location }); // you should use post instead of get to persist data on the backend
  } catch (err) {
    console.error(err);
  }
});

// 2 start the task
Location.startLocationUpdatesAsync(TASK_FETCH_LOCATION, {
  accuracy: Location.Accuracy.Highest,
  distanceInterval: 1, // minimum change (in meters) betweens updates
  deferredUpdatesInterval: 1000, // minimum interval (in milliseconds) between updates
  // foregroundService is how you get the task to be updated as often as would be if the app was open
  foregroundService: {
    notificationTitle: 'Using your location',
    notificationBody: 'To turn off, go back to the app and switch something off.',
  },
});

// 3 when you're done, stop it
Location.hasStartedLocationUpdatesAsync(TASK_FETCH_LOCATION).then((value) => {
  if (value) {
    Location.stopLocationUpdatesAsync(TASK_FETCH_LOCATION);
  }
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: leave page 
Javascript :: create new record mongoose 
Javascript :: javascript js ternary operater 
Javascript :: mongodb js insertmany 
Javascript :: WebPack Multiple files 
Javascript :: discord.js v13 joinVoiceChannel 
Javascript :: while loop vs for loop javascript 
Javascript :: jsconfig 
Javascript :: How to submit form with enter press in html textarea 
Javascript :: angular javascript 
Javascript :: how to add a class in classlist and remove after 100 ms using jquery 
Javascript :: jsfuck 
Javascript :: find the length of checked in js 
Javascript :: today tomorrow day after tomorrow button html and javascript 
Javascript :: javascript round down to 2 decimal places 
Javascript :: delay / sleep program in js 
Javascript :: how to format a javascript date 
Javascript :: same date to string in javascript minus and days difference 
Javascript :: ipcrenderer preload.js 
Javascript :: fcm node 
Javascript :: The data option must be a function. Plain object usage is no longer supported. vue 
Javascript :: check internet connection in react 
Javascript :: js hide element 
Javascript :: javscript randomly generate 89digit number 
Javascript :: deploy react to azure : web.config 
Javascript :: node.js process.argv 
Javascript :: Get async: false 
Javascript :: delegate click in jquery 
Javascript :: javascript check type of variable var 
Javascript :: leaflet flyto 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =