0

Usecase:跟蹤關聯位置,只要他登錄,但稍後關閉應用程序。Cordova Background地理位置服務器更新

使用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"); 
    } 

回答

0

嘗試使用'網址' 的插件選項。 不要指望你的回調每次都有效,因爲你的應用程序活動可能會在操作系統的後臺被殺死,這也會導致你的回調消失。

除此之外,服務應該生存的殺手,因此如果使用網址的插件選項,您仍然可以獲取服務器上更新