获取工作离子+ ngCordova +后台地理定位

gwbalxhn  于 2022-10-31  发布在  Angular
关注(0)|答案(2)|浏览(234)

**应用程序的目标:**获取每次移动的地理位置,并在应用程序处于前台和后台时记录位置。

我已经尝试了这么多的代码和组合,但我不能管理有它的工作(2天从现在开始...)。
经典的地理定位(getCurrentPosition)运行良好,但当我们关闭应用程序时,后台地理定位启动,但什么也没发生...函数“callbackFn”从未被触发。
我在IOS上测试xcode〉Capabilities Audio & location激活后台活动。我还在插件中给出了jQuery示例,所以我看到它工作,但从来没有与ionic/angularjs。
下面是处理背景的当前控制器:

.controller('TestCtrl', function($scope, $timeout, $cordovaBackgroundGeolocation, $ionicPlatform, $window)
{
$scope.lat_geo = "loading lat...";
$scope.long_geo = "loading long...";

//-- Geolocal launch
var options = {
    enableHighAccuracy : false,
    desiredAccuracy: 0,
    stationaryRadius: 1,
    distanceFilter: 5,
    notificationTitle: 'Background tracking', // <-- android only, customize the title of the notification
    notificationText: 'ENABLED', // <-- android only, customize the text of the notification
    activityType: 'AutomotiveNavigation',
    debug: true, // <-- enable this hear sounds for background-geolocation life-cycle.
    stopOnTerminate: false // <-- enable this to clear background location settings when the app terminates
};

$ionicPlatform.ready(function()
{
    console.log("[IONIC PLATFORM IS NOW READY]");

    //-- First launch a basic geolocalisation to get user acceptance of geosharing ;)
    navigator.geolocation.getCurrentPosition(function(location) {
            console.log('[GEOLOCAL JS1] Location from Phonegap');
    },
    function (error){
            console.log('[GEOLOCAL JS1] error with GPS: error.code: ' + error.code + ' Message: ' + error.message);
    },options);

    //-- test adaptation depuis l'app jquery
    var callbackFn = function(location) {
            console.log('[BackgroundGeoLocation] Update callback:  ' + location.latitude + ',' + location.longitude);
    };

    var failureFn = function(error) {
            console.log('[BackgroundGeoLocation] Error: '+error);
    };

    $cordovaBackgroundGeolocation.configure(callbackFn, failureFn, options);

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

    //-- Just a timeout to retreive long / lat
    $timeout(function()
    {
        navigator.geolocation.getCurrentPosition(function(location)
        {
            console.log('[GEOLOCAL JS3] Location from Phonegap');
            startPos = location;
            $scope.$apply(function () {
                $scope.lat_geo = startPos.coords.latitude;
                $scope.long_geo = startPos.coords.longitude;
            });
            console.log("[GEOLOCAL BASIC] OK this time :)");
        },
        function (error){
            console.log('[GEOLOCAL JS3] error with GPS: error.code: ' + error.code + ' Message: ' + error.message);
        },options);
    }, 3000);

});
//-- End Geolocal
})

我已经把我所有的代码(一个完整的离子应用程序启动器)放在github上:https://github.com/Jeff86/ionic_ngcordova_backgroundgeo_test/tree/master

fbcarpbf

fbcarpbf1#

我读了这篇文章http://ngcordova.com/docs/plugins/backgroundGeolocation/
我觉得你应该把你的代码
document.addEventListener("deviceready", function () { ... });
你找到解决办法了吗?

m1m5dgzv

m1m5dgzv2#

你肯定不想在bg中使用标准的 cordova 地理定位插件,它会在任何时候杀死电池。
我是Ionic的底层背景地理定位插件的作者。我创建了一个新的基于Ionic的SampleApp。
https://github.com/transistorsoft/cordova-background-geolocation-SampleApp

相关问题