I am using the example code given on the react-native-background-geolocation library home page:
import React from 'react';
import {
Switch,
Text,
View,
} from 'react-native';
import BackgroundGeolocation, {
Location,
Subscription
} from "react-native-background-geolocation";
export default class HelloWorld extends React.Component {
subscriptions:Subscription[] = [];
state:any = {};
constructor(props:any) {
super(props);
this.state = {
enabled: false,
location: ''
}
}
componentDidMount() {
/// 1. Subscribe to BackgroundGeolocation events.
this.subscriptions.push(BackgroundGeolocation.onLocation((location) => {
console.log('[onLocation]', location);
this.setState({location: JSON.stringify(location, null, 2)})
}, (error) => {
console.log('[onLocation] ERROR:', error);
}))
this.subscriptions.push(BackgroundGeolocation.onMotionChange((event) => {
console.log('[onMotionChange]', event);
}))
this.subscriptions.push(BackgroundGeolocation.onActivityChange((event) => {
console.log('[onActivityChange]', event);
}))
this.subscriptions.push(BackgroundGeolocation.onProviderChange((event) => {
console.log('[onProviderChange]', event);
}))
/// 2. ready the plugin.
BackgroundGeolocation.ready({
// Geolocation Config
desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
distanceFilter: 10,
// Activity Recognition
stopTimeout: 5,
// Application config
debug: true, // <-- enable this hear sounds for background-geolocation life-cycle.
logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
stopOnTerminate: false, // <-- Allow the background-service to continue tracking when user closes the app.
startOnBoot: true, // <-- Auto start tracking when device is powered-up.
// HTTP / SQLite config
url: 'http://yourserver.com/locations',
batchSync: false, // <-- [Default: false] Set true to sync locations to server in a single HTTP request.
autoSync: true, // <-- [Default: true] Set true to sync each location to server as it arrives.
headers: { // <-- Optional HTTP headers
"X-FOO": "bar"
},
params: { // <-- Optional HTTP params
"auth_token": "maybe_your_server_authenticates_via_token_YES?"
}
}).then((state) => {
this.setState({enabled: state.enabled});
console.log("- BackgroundGeolocation is configured and ready: ", state.enabled);
})
}
/// When view is destroyed (or refreshed during development live-reload),
/// remove BackgroundGeolocation event subscriptions.
componentWillUnmount() {
this.subscriptions.forEach((subscription) => subscription.remove());
}
onToggleEnabled(value:boolean) {
console.log('[onToggleEnabled]', value);
this.setState({enabled: value})
if (value) {
BackgroundGeolocation.start();
} else {
this.setState({location: ''});
BackgroundGeolocation.stop();
}
}
render() {
return (
<View style={{alignItems:'center'}}>
<Text>Click to enable BackgroundGeolocation</Text>
<Switch value={this.state.enabled} onValueChange={this.onToggleEnabled.bind(this)} />
<Text style={{fontFamily:'monospace', fontSize:12}}>{this.state.location} </Text>
</View>
)
}
}
But I am facing the issue, when I start tracking initially it gives 2 response like below:
{
"coords": {
"ellipsoidal_altitude": 0,
"speed_accuracy": 0,
"longitude": -122.02994396,
"speed": 3.56,
"floor": 0,
"heading_accuracy": 0,
"latitude": 37.33067786,
"accuracy": 10,
"heading": 90.76,
"altitude": 0,
"altitude_accuracy": -1
},
"extras": {},
"is_moving": false,
"event": "motionchange",
"odometer": 7897.8,
"age": 776,
"uuid": "0B7B6369-4C31-45C8-ADC3-2A85DFBF3040",
"battery": {
"level": -1,
"is_charging": false
},
"activity": {
"type": "unknown",
"confidence": 100
},
"timestamp": "2024-02-29T05:00:02.372Z"
}
Then for a few seconds, I got no response for location change even though my location is continuously changing.
Does anyone have any idea about how can I get continuous responses from the start?