首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为安卓安装PhoneGap后台位置插件?

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

Stack Overflow用户
提问于 2014-05-14 19:40:20
回答 2查看 4K关注 0票数 1

我在Stackoverflow网站上发现了类似的问题,但他们都回答了我的一个问题,但打开了一些我不知道的问题。

我想为我的PhoneGap 3.3.0应用程序使用地理位置插件。我想在地址科多瓦-插件-背景-地理定位上使用插件

好的,安装上说它可以和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中加入了这个代码(基本上和插件中说的一样):

代码语言:javascript
复制
<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手机上时,我只得到了一个警报‘设备就绪’,但是任何其他我没有收到的警报(您可以看到,我已经在每个位置设置了一个警报来测试它)。

好的,上面说我有两个问题。

1.这个插件是否与PhoneGap 3.3.0兼容,我如何检查?

2.除了安装带有phonegap plugin add https://github.com/christocracy/cordova-plugin-background-geolocation.git的插件外,我还需要更新哪些文件才能使这个插件工作(plugin.xml、config.xml等)还有愤怒的价值观。我已经看到了一个关于添加gap:plugin的文档,但是我的编译器在抱怨。所以总的来说,我该怎么做才能让这件事奏效呢?

另外,重要的是,我找到了一篇关于使用这个插件的文章这里,但是我不知道该放在哪里,所以如果有人能帮我的话,可能会很有帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-25 09:12:17

好的。我找到了答案。

关键的词是local。这个插件需要安装local word:

代码语言:javascript
复制
phonegap local plugin add https://github.com/christocracy/cordova-plugin-background-geolocation.git

到目前为止,在Phonegap3.3上,它创造的一切都像预期的那样完美。按照插件网站上的进一步说明,你就可以走了。另外,如果您正在使用Angular.js,那么如果您想在某些服务中使用它,则在依赖项列表中添加“$window”。例如:

代码语言:javascript
复制
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();
   }
}
票数 2
EN

Stack Overflow用户

发布于 2014-05-15 04:43:11

请在cordova_plugin.js中声明您的插件配置。

代码语言:javascript
复制
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"
    }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23663690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档