Cordova背景GeoLocation服务器更新

问题描述 投票:0回答:2

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

使用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");
    }
android cordova ionic-framework geolocation android-intentservice
2个回答
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
      });



   }


});

0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.