christocracy / cordova-plugin-background-geolocation

Sophisticated, battery-conscious, cross-platform background-geolocation with motion-detection
http://transistorsoft.github.io/cordova-background-geolocation
492 stars 743 forks source link

Json Request template #259

Closed cissemy closed 7 years ago

cissemy commented 7 years ago

Right now i have the callback function

var callbackFn = function (location, taskId) {
            var coords = location.coords;
            var lat = coords.latitude;
            var lng = coords.longitude;
            var gps = { "Id": courier, "Latitude": lat, "Longitude": lng };
            $.ajax({
                type: "POST",
                url: Base_URL + "SetGPS",
                cache: false,
                data: JSON.stringify(gps),
                contentType: "application/json",
                dataType: "json",
                success: function (data) {

                },
                error: function (xhr) {
                     //  showToast('toast', 'Gps data upload Server Error!', 'error');
                }
            });
           setTimeout(function () {
                bgGeo.finish(taskId); // <-- execute #finish when your work in callbackFn is complete
            }, 1000);
        };

Do i have to remove my server call there and add :

url: Base_URL + "SetGPS",
  httpRootProperty: '.',
  locationTemplate: '{"Id": "0992", "lat":<%= latitude %>, "lng":<%= longitude %> }'

? Thanks

christocracy commented 7 years ago

Why are you doing this??

setTimeout(function () { 
            bgGeo.finish(taskId); // <-- execute #finish when your work in callbackFn is complete
        }, 1000);

You're supposed to understand that setTimeout there merely a SIMULATION of a long-running task, like an Ajax request. You're not actually supposed to wrap bgGeo.finish in a setTimeout

$.post(url, function(response) {  // <-- long running task
  bgGeo.finish(taskId);
});
christocracy commented 7 years ago

Custom JSON templates apply only the plugin's own HTTP service.

Yes, you're supposed to remove your Ajax post.

christocracy commented 7 years ago

And btw, you're posting this issue at the wrong repo. Look at the README of this repo.