【问题标题】:cordova - geolocation timed out when app in background科尔多瓦 - 当应用程序在后台时地理定位超时
【发布时间】:2016-05-02 09:00:33
【问题描述】:

我在 cordova 上创建了一个应用程序,这是一个在地图上寻找朋友的社交网络。当应用程序处于前台时(使用 cordova-plugin-geolocation 插件),地理定位工作正常。 但我也希望它在应用程序在后台运行时工作。为此,我使用了cordova-plugin-background-mode,以这种方式工作:

var geolocID;
window.onload = function(){
        document.addEventListener("deviceready", init, false);
        document.addEventListener("resume", function() { console.log("resume with id :" + geolocID); navigator.geolocation.clearWatch(geolocID); init();},false);
        document.addEventListener("pause", geolocBackground,false);//function() {setTimeout(function(){console.log("TEST");},5000)},false);
    }

    function geolocBackground()
    {
        console.log("In geolocaBackground " + ", ID = " + geolocID);
        navigator.geolocation.clearWatch(geolocID);
        console.log("ID = " + geolocID);

        geolocID = navigator.geolocation.watchPosition(positionSuccessBackground,positionError,{timeout: 5000});
        console.log("Geolocation end");
    }


    function init()
    {
        geolocID = navigator.geolocation.watchPosition(positionSuccess, positionError, {enableHighAccuracy: true, timeout: 60000, frequency: 10000});

        cordova.plugins.backgroundMode.enable();
        cordova.plugins.backgroundMode.onactivate = function(){console.log("Backgound mode activated");
                                                                cordova.plugins.backgroundMode.configure({silent: false});
                                                                };
        cordova.plugins.backgroundMode.ondeactivate = function() {console.log("Backgound mode disabled");};
    }

backgkound 模式似乎有效,因为当我暂停应用程序时,我的控制台中有这个:

 In geolocaBackground , ID = 707e54f8-fb99-fb36-2855-341b45f3d774
 ID = 707e54f8-fb99-fb36-2855-341b45f3d774
 Geolocation end
 resume with id :707e54f8-fb99-fb36-2855-341b45f3d774

但是地理定位不起作用,返回超时错误,代码 3.. 我不明白我的代码有什么问题,我在网上没有找到任何东西。貌似app无法在后台获取手机的位置信息。代码已执行,但只有定位功能不起作用。如果有人对此问题有所了解,将不胜感激:) 感谢您的帮助,问候

【问题讨论】:

    标签: cordova plugins background location timeout


    【解决方案1】:

    【讨论】:

    【解决方案2】:

    希望这与我遇到的问题相同。我的代码如下: start() { cordova.plugins.backgroundMode.enable(); var gpsOptions = { maximumAge: 2000, timeout: 5000, enableHighAccuracy: true }; document.addEventListener('deviceready', function () { var GpsLog = []; this.gpsChecker = Observable.timer(0, 4000); this.gpsChecker.subscribe(() => { navigator.geolocation.getCurrentPosition(position => { var deviceLog = { type: 'Device Location', value: position.coords.longitude + "," + position.coords.latitude } this._api.uploadLog(deviceLog); }, null, gpsOptions); }); }); }

    (我使用的是 ionic 2 和 typescript,但原理是一样的)。问题似乎是(无论如何我的问题)是当您的应用程序在后台时,它没有对设备 GPS 位置的“优先”访问权限。即,当我的应用程序进入后台时,我必须在外面走动(大约 15 米)才能获得地理位置,如果应用程序在前台运行,我每隔几秒钟就会获得一次地理位置。尝试降低超时时间和最大年龄参数,然后带着设备在晴朗的天空中散步。

    【讨论】:

      【解决方案3】:

      感谢您的回答。 我没有成功地使地理定位在后台工作,有或没有铬,或步行几米。 我自己解决了这个问题,用 Java 编写了一个 Android 服务和一个 gps 跟踪器,所以当应用关闭时,我的应用仍然会跟踪我的用户。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 2018-07-20
        • 1970-01-01
        • 1970-01-01
        • 2015-05-31
        • 1970-01-01
        相关资源
        最近更新 更多