离子应用程序背景中的地理位置报告

时间:2016-04-06 11:41:28

标签: android cordova ionic-framework

即使应用程序在后台处于休眠状态并将其存储在数据库中,我也打算获得用户的地理位置。 我正在使用katzer的Cordova Background Plug-in, 当我尝试在navigator.geolocation.getCurrentPosition函数内部访问backgroundMode.onactivate时,没有任何反应,而当我尝试传递硬编码值时,会调用api,数据存储在数据库中。

以下是我的代码

document.addEventListener('deviceready', function() {
    // Android customization
    cordova.plugins.backgroundMode.setDefaults({
        text: 'Doing heavy tasks.'
    });
    // Enable background mode
    cordova.plugins.backgroundMode.enable();

    // Called when background mode has been activated
    cordova.plugins.backgroundMode.onactivate = function() {
        console.log('inside background')

        a();
    }

    var a = function() {
        console.log('a called')
        navigator.geolocation.getCurrentPosition(function(pos) {

            console.log('inside navigate');
            var data = {
                Lati: '123456',
                Longi: '132456',
                //LoginID: JSON.parse(window.localStorage.getItem('LoginId'))
                EmpCode: localStorage.getItem('LoginId')
            };

            $http.post("https://app.sbismart.com/bo/ContactManagerApi/UpdateEmployeeLoc", data).success(function(rsdata, status) {
                console.log('inside rsdata');
                console.log(data.Lati + "," + data.Longi);
            })
        }, function(error) {
            alert('Unable to get location: ' + error.message);
        });
    }


}, false);

1 个答案:

答案 0 :(得分:1)

cordova.plugins.backgroundMode.onfailure = function(errorCode) {
console.log(errorCode)
};`

并检查它为什么失败....然后再次需要在后台的超时函数中运行locationService函数以获取有关位置的更新并检查先前获取位置的位置。 像这样......

cordova.plugins.backgroundMode.onactivate = function () {
        setTimeout(function () {
           a();

        }, 5000);
    }

希望这有帮助。