react-native -v): 0.46.4global.watchLocation = function(){
BackgroundGeolocation.configure({
desiredAccuracy: 0,
distanceFilter: 50,
foregroundService: true,
notificationPriority: BackgroundGeolocation.NOTIFICATION_PRIORITY_MAX,
desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
forceReloadOnBoot:true,
forceReloadOnSchedule:true,
stopOnTerminate: false,
startOnBoot:true,
deferTime:10,
notificationText: "Location service at trip",
locationUpdateInterval: 3000,
distanceFilter: 0,
}, function(state) {
console.log('- BackgroundGeolocation configured and ready', state);
global.registerBackgroundJob();
if (!state.enabled) {
BackgroundGeolocation.start();
}
});
}
global.registerBackgroundJob = function(){
BackgroundJob.register({
jobKey: 'watchLocation',
job: () => global.sendPosition()
});
BackgroundJob.schedule({
jobKey: 'watchLocation',
period: 3000,
exact: true,
allowExecutionInForeground: true,
timeout: 150000,
});
}
global.sendPosition = function(){
global.getItem(global.ACCESS_TOKEN, function(s,token){
if(s){
BackgroundGeolocation.getCurrentPosition((pos) => {
console.log(token, pos);
var param = {
APIKEY: token,
Lat: pos.coords.latitude,
Lng: pos.coords.longitude,
Speed: pos.coords.speed,
Time: moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
}
var dataParam = param;
param = global.convertDataURL(param);
global.setTimeOut(fetch(global.base_url+'api/sendtripcoordinate', {
method: 'POST',
headers: global.requestHeader,
body: param
}))
.then((response) => {
return response.json();
})
.then((data) => {
if(data.error){
commonAlert(data.error ? data.error: "Please check your internet connection", "Warning");
}
else{
global.getItem('tempCoordinate', (status,value) => {
if(status && value){
let result = JSON.parse(value);
result.map(function(i){
var data = global.convertDataURL(i);
global.setTimeOut(fetch(global.base_url+'api/sendtripcoordinate', {
method: 'POST',
headers: global.requestHeader,
body: data
}))
.then((response) => {return response.json();})
.then((data)=>{
console.log("berhasil konek simpan data dari localstorage ke server",data);
})
});
global.removeItem('tempCoordinate', s => {});
}
})
}
})
.catch((error) => {
//commonAlert(error ? error: "Please check your internet connection", "Warning");
console.log("fail to server coordinate, save to localstorage");
var inputData = [];
inputData.push(dataParam);
global.getItem('tempCoordinate', (status,value) => {
if(status && value){
let result = JSON.parse(value);
result.map(function(i){
inputData.push(i);
});
console.log(inputData);
}
global.setItem('tempCoordinate', JSON.stringify(inputData), s => {});
})
})
},(err) => {
console.log("Error dari geoLocation",err)
}, global.geoLocationOption);
}
});
}
Add you aware the plugin has its own database and http service? The plugin persists every location to its own SQLite database until its http service receives a 20x response from your server or you manually #destroyLocations.
You only need to provide the plugin an optional #url, #params, #headers.
The plugin was designed to track first-responders in disaster zones where network is assumed to be destroyed. The plugin is designed for long-term tracking as if life depended on it. When the plugin senses a valid http connection, it can automatically upload its stored locations from its database to your configured server.
You do not need to implement your own client persistence mechanism or http service.
If you are experienced unusual behavior, the plugin logs everything it does to its log database. You have not provided any logs, so there’s little to speculate about until you do. See the “Debugging” section of WIKI.
i got it.
it's done.
how to set if is_moving = false, data not sent to the server ?
how to set if is_moving = false
I don't understand your question.
When is_moving: false, the plugin stops recording locations. If the device isn't moving, the plugin turns OFF location-services and stops recording locations.
Read the Philosophy of Operation
Most helpful comment
Add you aware the plugin has its own database and http service? The plugin persists every location to its own SQLite database until its http service receives a 20x response from your server or you manually #destroyLocations.
You only need to provide the plugin an optional #url, #params, #headers.
The plugin was designed to track first-responders in disaster zones where network is assumed to be destroyed. The plugin is designed for long-term tracking as if life depended on it. When the plugin senses a valid http connection, it can automatically upload its stored locations from its database to your configured server.
You do not need to implement your own client persistence mechanism or http service.
If you are experienced unusual behavior, the plugin logs everything it does to its log database. You have not provided any logs, so there’s little to speculate about until you do. See the “Debugging” section of WIKI.