Question

I have found similar questions to this one on Stackoverflow, but all of them answers to one of my questions, but opens a number of ones that I don't really know.

I wanted to use a geo-location plugin for my PhoneGap 3.3.0 application. I wanted to use plugin on address cordova-plugin-background-geolocation

Okay, the install says that it can be used with plugman, so I have installed plugman and installed it with it. (also I have used a phonegap install, instead of cordova, as I am using phonegap plugin plugin add https://github.com/christocracy/cordova-plugin-background-geolocation.git) . So, ok, the new Java code is now placed in /platforms/android/src/com/tenforwardconsulting/cordova/bgloc/BackgroundGpsPlugin.java

I have put into my index.html this code (basically the same as said in the plugin):

<script type="text/javascript">
document.addEventListener("deviceready", onDeviceReady, true); 


    function onDeviceReady() {
            alert('Device ready');


    //
    // after deviceready
    //
    //


    var bgGeo = window.plugins.backgroundGeoLocation;

    // BackgroundGeoLocation is highly configurable.
    bgGeo.configure(callbackFn, failureFn, {
        url: 'http://only.for.android.com/update_location.json', // <-- only required for Android; ios allows javascript callbacks for your http
        authToken: 'user_secret_auth_token',    // <-- only required for Android; ios allows javascript callbacks for your http
        desiredAccuracy: 10,
        stationaryRadius: 20,
        distanceFilter: 30,
        debug: true // <-- enable this hear sounds for background-geolocation life-cycle.
    });

    // Turn ON the background-geolocation system.  The user will be tracked whenever they suspend the app.
    bgGeo.start();


}

// Your app must execute AT LEAST ONE call for the current position via standard Cordova geolocation,
    //  in order to prompt the user for Location permission.
    window.navigator.geolocation.getCurrentPosition(function(location) {
        console.log('Location from Phonegap');
         alert("Get current location");
    });


    /**
    * This would be your own callback for Ajax-requests after POSTing background geolocation to your server.
    */
    var yourAjaxCallback = function(response) {
        ////
        // IMPORTANT:  You must execute the #finish method here to inform the native plugin that you're finished,
        //  and the background-task may be completed.  You must do this regardless if your HTTP request is successful or not.
        // IF YOU DON'T, ios will CRASH YOUR APP for spending too much time in the background.
        //
        //
        alert("Callback function");
        //bgGeo.finish();
    };

    /**
    * This callback will be executed every time a geolocation is recorded in the background.
    */
    var callbackFn = function(location) {
        console.log('[js] BackgroundGeoLocation callback:  ' + location.latitudue + ',' + location.longitude);
        // Do your HTTP request here to POST location to your server.
        //
        //
        //yourAjaxCallback.call(this);
        alert("Location recorded");
    };

    var failureFn = function(error) {
        console.log('BackgroundGeoLocation error');
        alert("BackgroundGeoLocation error");
    }



    // If you wish to turn OFF background-tracking, call the #stop method.
    // bgGeo.stop()

    </script>

When I build phonegap and deploy to the Android phone I only get an alert 'Device ready', but any other alert I don't get (You can see I have put on every location an alert just to test it)

Ok, having that said I have two questions.

1. Is this plugin even compatible with PhoneGap 3.3.0 and how I can check that ?

2. Except installing a plugin with phonegap plugin add https://github.com/christocracy/cordova-plugin-background-geolocation.git what files I have to update to make this plugin workable (plugin.xml, config.xml etc.) and with wath values. I have seen a documentation about adding gap:plugin, but my compiler is complaining. So in general what should I do to make this to work ?

Also, important, I have found an article here about using this plugin, but I didn't understand what to put where, so it could be helpful if somebody could help me with it.

Was it helpful?

Solution

Ok. I have found an answer.

The crucial word is local. The plugin needs to be installed with local word:

phonegap local plugin add https://github.com/christocracy/cordova-plugin-background-geolocation.git

So far, on Phonegap 3.3 it created all perfectly as expected. Follow the further instructions from the plugin's web-site and you are good to go. A side note, if you are using Angular.js then if you would like to use this inside some service, add '$window' in list of your dependencies. E.g.:

angular
.factory('Auth', [ '$http','$q', '$window', '$rootScope', '$location', 'Application', 'Service', function($http,$q, $window, $rootScope, $location, Application, Service){
// Some code and then:

function callbackFn(){
//callback for ios
}

function failureFn(){
//callback for ios
}


function connect(){
   var bgGeo = $window.plugins.backgroundGeoLocation;

   bgGeo.configure(callbackFn, failureFn, {
                    url: "www.yoursite.com/api/location_saver.json", 
                    params: {      // HTTP POST params sent to your server when persisting locations. It will be posted to your server as is , so you can set any parameter you want
                        user_credentials: "aS9393498asdFfaSDF",
                        parameter2: "another parameter value I want to send" 
                    },
                    desiredAccuracy: 10,
                    stationaryRadius: 1,
                    distanceFilter: 1,
                    debug: true // <-- enable this hear sounds for background-geolocation life-cycle.
                });


   bgGeo.start();
   }
}

OTHER TIPS

Please declare your plugin configuration in cordova_plugin.js.

cordova.define('cordova/plugin_list', function(require, exports, module) {
    module.exports = [{
        "file": "plugins/cordova-plugin-background-geolocation/www/xxx.js",
        "id": "cordova-plugin-background-geolocation",
        "merges": ["window.plugins.backgroundGeoLocation"]
    }];
    module.exports.metadata = // TOP OF METADATA 
    {
        "cordova-plugin-background-geolocation": "0.0.0"
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top