【问题标题】:Get working Ionic + ngCordova + background geolocation开始工作 Ionic + ngCordova + 背景地理定位
【发布时间】:2015-04-23 23:56:16
【问题描述】:

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

我已经尝试了很多代码和组合,但我无法让它工作(从现在起 2 天......)。

经典的地理定位 (getCurrentPosition) 工作正常,但当我们关闭应用程序时,后台地理定位已启动,但没有任何反应...函数“callbackFn”从未被触发。

我正在使用 xcode 在 IOS 上进行测试 > 功能音频和位置已为后台活动激活。我还制作了插件中给出的 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
})

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

【问题讨论】:

    标签: ios angularjs geolocation ionic-framework ngcordova


    【解决方案1】:

    我看了这篇文章http://ngcordova.com/docs/plugins/backgroundGeolocation/

    我看到你应该把你的代码放进去

    document.addEventListener("deviceready", function () { ... });

    你找到解决办法了吗?

    【讨论】:

    • 嗨,我确实尝试过使用 deviceready 进行封装,但没有成功。您可以在 controller.js 上看到我在第 70 行的评论中添加了它。现在我只是放弃使用这个 bglocation,因为我没有时间花在它上面,也许几个月后。
    【解决方案2】:

    你绝对不想在 bg 中使用 std Cordova-geolocation 插件,它会很快耗尽电池。

    我是 Ionic 底层背景地理定位插件的作者。我创建了一个基于 Ionic 的新 SampleApp。

    https://github.com/transistorsoft/cordova-background-geolocation-SampleApp

    【讨论】:

    • RTFM,有明确的记录。顺便说一句,不再支持该 Android 版本。它像野餐时的蚂蚁一样吃电池。我不建议将其用于生产应用程序,尤其是在您希望用户喜欢您的情况下。
    • 是的,我找到了。不过,如果您在代码示例中说明对于平台根本不起作用的东西,那将是一件好事。而不是详细地写它,并在behavior 的最后一章中提到某些东西实际上根本不起作用。无论如何,我只需要每隔 30 分钟检查一次位置。
    • 您使用的版本不再受支持(至少 6 个月没有支持)。新的仓库在这里github.com/transistorsoft/cordova-background-geolocation-lt
    • 如果您还有其他问题,最好直接在 repo 中提出问题。
    猜你喜欢
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 2019-07-16
    • 2021-12-29
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多