Cordova背景GeoLocation服务器更新

时间:2017-02-21 07:27:22

标签: android cordova ionic-framework geolocation android-intentservice

用例:跟踪关联位置,一旦他登录,但稍后关闭应用程序。

使用https://github.com/mauron85/cordova-plugin-background-geolocation插件。 在调试模式下,它显示值,但是,在回调函数中,它没有进行服务器调用。 renderMaps函数调用navigator.geolocation.getCurrentPosition

document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady() {
        // Now safe to use device APIs
        renderMaps();

        var callbackFn = function(location) {
            var data = 'longitude='+ location.longitude + '&latitude=' + location.latitude + '&id=' + vm.user_id + '&token=' + vm.accessToken;
            window.longitude_sel = location.latitude;
            window.latitude_sel = location.longitude;
            console.log("" + data);
             $.ajax({
                 type: "POST",
                 url: "https://example.com/partner/location",
                 data: data,
                 success: function(response){
                        console.log("RESPONSE" + response);
                      }
                  });
            backgroundGeolocation.finish();
        };

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

        // BackgroundGeolocation is highly configurable. See platform specific configuration options
        backgroundGeolocation.configure(callbackFn, failureFn, {
            desiredAccuracy: 5,
            stationaryRadius: 0,
            distanceFilter: 30,
            interval: 60000,
            stopOnTerminate: false,
            startOnBoot: false,
            startForeground: true,
            stopOnStillActivity: false,
            debug: true
        });

        backgroundGeolocation.start();
        console.log("TEST");
    }

2 个答案:

答案 0 :(得分:1)

尝试此解决方案。以我为例:

    $$(document).on('deviceready', function() {



   cordova.plugins.backgroundMode.enable();
   alert('device working!');
   var userId=localStorage.getItem("userId");
   if(userId!=null)
   {
         BackgroundGeolocation.configure({
            locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER,
            desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY,
            stationaryRadius: 50,
            distanceFilter: 50,
            notificationTitle: 'Background tracking',
            notificationText: 'enabled',
            debug: true,
            interval: 10000,
            fastestInterval: 5000,
            activitiesInterval: 10000,
            url: 'http://example.com/index.php/locations/savebackgroundlocation',
            httpHeaders: {
              "Authorization": "Basic " + btoa('stack' + ":" + 'pwd@123#')
            },
            // customize post properties
            postTemplate: {
              lat: '@latitude',
              lon: '@longitude'

            }
      });

      BackgroundGeolocation.checkStatus(function(status) {
          BackgroundGeolocation.start(); //triggers start on start event
      });



   }


});

答案 1 :(得分:0)

尝试使用插件的' 选项。 不要期望你的回调每次都能正常工作,因为操作系统可能会在后台杀死你的应用活动,这也会导致你的回调。

除此之外,服务应该在kill中存活,所以如果你使用插件的 url 选项,你仍然可以在服务器上获取你的更新