【问题标题】:Cordova Background GeoLocation PluginCordova 背景地理定位插件
【发布时间】:2015-01-11 02:20:32
【问题描述】:

我正在将这个 cordova 插件 https://github.com/christocracy/cordova-plugin-background-geolocation/ 用于 ios 应用程序,但遇到了一些问题。如果有人可以提供帮助将不胜感激。

这是我使用的插件示例本身的示例代码。

问题是当检测到位置更改时似乎没有调用回调函数 (callbackFn),但我确实在 Iphone 上收到了通知,并且我看到其中一个通知是未完成的 bg-task 被强制终止。不知道这到底是什么意思...

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
     bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },

onDeviceReady: function() {
    app.receivedEvent('deviceready');
     if (window.plugins.backgroundGeoLocation) {
        app.configureBackgroundGeoLocation();
      }
},
// Update DOM on a Received Event
receivedEvent: function(id) {
    window.cache.clear( function(){console.log("cache cleared");}, function(){console.log("cache could not be cleared");} );
    require(['http://localhost:8080/sdm/app_resources/app_product_HY6I1FJ5KGRD/resources/js/main.js']);

},
configureBackgroundGeoLocation: function() {
        // 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');
             });

            var bgGeo = window.plugins.backgroundGeoLocation;

      /** This would be your own callback for Ajax-requests after POSTing background geolocation to your server.*/

            var yourAjaxCallback = function(response) {
                    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.
                 deviceId = 'I2FKRKXX8SQ0';
                 trackingToken = 'SomeSecurityToken';
                 jsonData={location:{longitude:location.longitude,latitude:location.latitude}};
                 var encodedToken = Base64.encode('tracker' + ':' + trackingToken);
                 var http = new XMLHttpRequest();
                 var url = "http://localhost:8080/sdm/v1/KC/host/device/"+deviceId;

                 var params = JSON.stringify(jsonData);
                 http.open("PUT", url, true);

                 //Send the proper header information along with the request
                 http.setRequestHeader("Content-type", "application/json");
                 http.setRequestHeader("accept", "json");
                 http.setRequestHeader("Authorization", "Basic "+encodedToken);
                 http.setRequestHeader("Content-length", params.length);
                 http.setRequestHeader("Connection", "close");

                /* http.onreadystatechange = function() {//Call a function when the state changes.
                 if(http.readyState == 4 && http.status == 200) {
                 alert(http.responseText);
                 }
                 }*/
                 http.send(params);

                yourAjaxCallback.call(this);
            };

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

            // BackgroundGeoLocation is highly configurable.
            bgGeo.configure(callbackFn, failureFn, {
                 desiredAccuracy: 10,
                 stationaryRadius: 20,
                 distanceFilter: 30,
                 activityType: 'AutomotiveNavigation',
                 debug: true, // <-- enable this hear sounds for background-geolocation life-cycle.
                 stopOnTerminate: false
               });

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

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

};

【问题讨论】:

    标签: ios cordova


    【解决方案1】:

    如果你愿意,你可以试试我的后台插件,它使用纯 javascript 回调用于 ios 和 android。

    https://github.com/pmwisdom/cordova-background-geolocation-services

    【讨论】:

    • 嗨,看起来不错!不过,我有一个问题,您是否已成功向 Ap​​ple App Store 提交了应用程序?我听说他们不允许 Cordova BG 服务!
    • 我有两个使用我的后台插件的生产应用程序。它只是在下面使用他们自己的库,所以不应该有任何东西阻止你提交应用程序。
    • 太棒了!感谢您的插件和回复:)
    • 你的图书馆很棒!
    • 即使应用关闭我们也能追踪位置吗?
    【解决方案2】:

    检查你是否使用安卓。除非你有 Christrocracy 插件的高级版本,否则它不会执行你的回调函数。您必须指定url 来为安卓设备发送位置。

    Christocracy Background Geooction Plugin

    【讨论】:

      【解决方案3】:

      你还必须在开始时添加回调函数和失败函数。我有同样的问题。 例如。配置:bgGeo.configure(callbackFn, failureFn... bgGeo.start();必须改成bgGeo.start(callbackFn, failureFn);

      【讨论】:

        猜你喜欢
        • 2017-05-12
        • 2023-03-04
        • 1970-01-01
        • 2016-02-25
        • 1970-01-01
        • 1970-01-01
        • 2016-12-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多