【问题标题】:Cordova Background GeoLocation Server UpdateCordova 后台地理位置服务器更新
【发布时间】:2017-02-21 07:27:22
【问题描述】:

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

使用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


    【解决方案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
          });
    
    
    
       }
    
    
    });
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        我必须授予 ACCESS_BACKGROUND_LOCATION 权限

        在 plugins/cordova-plugin-geolocation/plugin.xml 文件中你需要添加:

           <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
        

        在此处查看更多信息:https://developer.android.com/training/location/background

        【讨论】:

          猜你喜欢
          • 2022-01-10
          • 2017-03-03
          • 1970-01-01
          • 1970-01-01
          • 2018-02-26
          • 2015-07-28
          • 1970-01-01
          • 2013-03-01
          • 1970-01-01
          相关资源
          最近更新 更多