如何为Android安装PhoneGap后台位置插件?

时间:2014-05-14 19:40:20

标签: cordova

我在Stackoverflow上找到了类似的问题,但是所有这些问题都回答了我的一个问题,但却打开了一些我不太了解的问题。

我想为我的PhoneGap 3.3.0应用程序使用地理位置插件。我想在地址cordova-plugin-background-geolocation

上使用插件

好的,安装说它可以和plugman一起使用,所以我安装了plugman并随之安装了它。 (我也使用了phonegap安装,而不是cordova,因为我使用的是phonegap plugin plugin add https://github.com/christocracy/cordova-plugin-background-geolocation.git)。所以,好的,新的Java代码现在放在/platforms/android/src/com/tenforwardconsulting/cordova/bgloc/BackgroundGpsPlugin.java

我已将index.html此代码加入(基本上与插件中所述相同):

<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>

当我构建phonegap并部署到Android手机时,我只收到“设备就绪”警报,但是我没有收到任何其他警报(你可以看到我已经在每个位置发出警报只是为了测试它)< / p>

好的,那就是说我有两个问题。

1。此插件是否与PhoneGap 3.3.0兼容,以及如何检查?

2。除了安装带有phonegap plugin add https://github.com/christocracy/cordova-plugin-background-geolocation.git的插件外,我必须更新哪些文件以使此插件可用(plugin.xml,config.xml等)并使用wath值。我看过有关添加gap:plugin的文档,但我的编译器正在抱怨。所以一般来说我该怎么做才能让它发挥作用?

另外,重要的是,我发现了一篇关于使用这个插件的文章here,但是我不知道该放什么地方,所以如果有人可以帮我的话,这可能会有所帮助。

2 个答案:

答案 0 :(得分:2)

确定。我找到了答案。

关键词是local。该插件需要安装local字:

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

到目前为止,在Phonegap 3.3上,它完全按照预期创建。按照插件网站的进一步说明,你很高兴。 请注意,如果您使用的是Angular.js,那么如果您想在某项服务中使用此功能,请添加&#39; $ window&#39;在您的依赖项列表中。 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();
   }
}

答案 1 :(得分:0)

请在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"
    }
});