importReact,{useEffect,useRef}from'react';import{Button,View}from'react-native';importBackgroundGeolocation,{Subscription,}from'react-native-background-geolocation';exportdefaultfunctionApp(){constsubscriptions=useRef<Subscription[]>([]);useEffect(()=>{constsubs=subscriptions.current;// Register event listeners *before* calling ready()subs.push(BackgroundGeolocation.onLocation(location=>{console.log('[onLocation]',location);}));// ready() configures the SDK and restores persisted state.// It does NOT start tracking — call start()/stop() separately.BackgroundGeolocation.ready({{{>ts-ready-config.md}}});return()=>subs.forEach(s=>s.remove());},[]);// e.g. a toggle-button handler in your UIconstonToggleTracking=async()=>{conststate=awaitBackgroundGeolocation.getState();if(state.enabled){BackgroundGeolocation.stop();}else{BackgroundGeolocation.start();}console.log('[state]',state.enabled,state.trackingMode);};return(<View><Buttontitle="Toggle Tracking"onPress={onToggleTracking}/></View>);}
import{Injectable,OnDestroy}from'@angular/core';importBackgroundGeolocation,{Subscription,}from'@transistorsoft/capacitor-background-geolocation';@Injectable({providedIn:'root'})exportclassBackgroundGeolocationServiceimplementsOnDestroy{privatesubscriptions:Subscription[]=[];init(){// Register event listeners *before* calling ready()this.subscriptions.push(BackgroundGeolocation.onLocation(location=>{console.log('[onLocation]',location);}));// ready() configures the SDK and restores persisted state.// It does NOT start tracking — call start()/stop() separately.BackgroundGeolocation.ready({{{>ts-ready-config.md}}});}// e.g. wired to a toggle button in your templateasynconToggleTracking(){conststate=awaitBackgroundGeolocation.getState();if(state.enabled){awaitBackgroundGeolocation.stop();}else{awaitBackgroundGeolocation.start();}console.log('[state]',state.enabled,state.trackingMode);}ngOnDestroy(){this.subscriptions.forEach(s=>s.remove());}}
importReact,{useEffect,useRef}from'react';importBackgroundGeolocation,{Subscription,}from'@transistorsoft/capacitor-background-geolocation';exportdefaultfunctionApp(){constsubscriptions=useRef<Subscription[]>([]);useEffect(()=>{constsubs=subscriptions.current;// Register event listeners *before* calling ready()subs.push(BackgroundGeolocation.onLocation(location=>{console.log('[onLocation]',location);}));// ready() configures the SDK and restores persisted state.// It does NOT start tracking — call start()/stop() separately.BackgroundGeolocation.ready({{{>ts-ready-config.md}}});return()=>subs.forEach(s=>s.remove());},[]);// e.g. a toggle-button handler in your UIconstonToggleTracking=async()=>{conststate=awaitBackgroundGeolocation.getState();if(state.enabled){BackgroundGeolocation.stop();}else{BackgroundGeolocation.start();}console.log('[state]',state.enabled,state.trackingMode);};return(<IonButtononClick={onToggleTracking}>ToggleTracking</IonButton>);}
<scriptsetuplang="ts">import{onMounted,onUnmounted}from'vue';importBackgroundGeolocation,{Subscription,}from'@transistorsoft/capacitor-background-geolocation';constsubscriptions:Subscription[]=[];onMounted(()=>{// Register event listeners *before* calling ready()subscriptions.push(BackgroundGeolocation.onLocation(location=>{console.log('[onLocation]',location);}));// ready() configures the SDK and restores persisted state.// It does NOT start tracking — call start()/stop() separately.BackgroundGeolocation.ready({{{>ts-ready-config.md}}});});// e.g. a toggle-button handler in your templateasyncfunctiononToggleTracking(){conststate=awaitBackgroundGeolocation.getState();if(state.enabled){awaitBackgroundGeolocation.stop();}else{awaitBackgroundGeolocation.start();}console.log('[state]',state.enabled,state.trackingMode);}onUnmounted(()=>{subscriptions.forEach(s=>s.remove());});</script>