Javascript Angular js CordovaGeology获取当前位置并在后台向服务器发送数据
我正在尝试CordovaGeology我在我的应用程序中插入了相同的代码。当应用程序打开时,我可以正常工作,我需要在应用程序处于后台时获取此地理位置并将其发送到服务器,因此使用后台模式cordova请参阅后台模式文档Javascript Angular js CordovaGeology获取当前位置并在后台向服务器发送数据,javascript,angularjs,cordova,background-mode,Javascript,Angularjs,Cordova,Background Mode,我正在尝试CordovaGeology我在我的应用程序中插入了相同的代码。当应用程序打开时,我可以正常工作,我需要在应用程序处于后台时获取此地理位置并将其发送到服务器,因此使用后台模式cordova请参阅后台模式文档 我得到了地理位置和发送到服务器是正常工作时,应用程序是开放的 我也使用了onpause和onresume cordova作为本机android中的应用程序状态 当应用程序处于onpause时,我使用了background enable()、inside onactivate()、给
app.run(['$rootScope'、'$location'、'$mdToast'、'$cordovaPush'、'$sessionStorage'、'$cordovdevice',
“$cordovaLocalNotification”、“$timeout”、“$cordovaGeolocation”、“$interval”、“Service”,
函数($rootScope、$location、$mdToast、$cordovaPush、$sessionStorage、$cordovaDevice、,
$cordovaLocalNotification、$timeout、$cordovaGeolocation、$interval、Service){
document.addEventListener(“resume”,函数(){
log(“应用程序正在从后台恢复”);
cordova.plugins.backgroundMode.disable();
},假);
document.addEventListener(“暂停”,函数(){
log(“应用程序正在暂停到后台”);
cordova.plugins.backgroundMode.enable();
},假);
var i=0;
cordova.plugins.backgroundMode.onactivate=函数(){
console.log(“notificationReceived”);
setInterval(函数(){
log(“间隔:+i++”);
var posOptions={timeout:3000,enableHighAccurance:false};
$Cordova地理定位
.getCurrentPosition(posOptions)
.然后(功能(位置){
var GeoLocation=新对象();
地理位置。地理位置纬度=位置坐标纬度;
geology.geo_location_longitude=position.coords.longitude;
Service.CurrentService(地理位置).success(函数(数据){
console.log(新日期());
log(“cur-loc:+JSON.stringify(数据));
}).错误(函数(数据){
log(“错误:+JSON.stringify(数据));
})
},函数(err){
日志(“错误:+err”);
//此处发生位置错误
//错误
});
},60000);
}
})];代码>您可以尝试以下方法:
app.run(['$rootScope', '$location','$mdToast','$cordovaPush', '$sessionStorage', '$cordovaDevice',
'$cordovaLocalNotification', '$timeout','$cordovaGeolocation','$interval','Service',
function($rootScope, $location,$mdToast,$cordovaPush, $sessionStorage, $cordovaDevice,
$cordovaLocalNotification, $timeout,$cordovaGeolocation,$interval,Service) {
document.addEventListener("resume", function() {
console.log("The application is resuming from the background");
cordova.plugins.backgroundMode.disable();
}, false);
document.addEventListener("pause", function() {
console.log("The application is pausing to the background");
cordova.plugins.backgroundMode.enable();
}, false);
var i=0;
cordova.plugins.backgroundMode.onactivate = function () {
console.log("notificationReceived");
setInterval(function () {
console.log("interval: "+ i++);
var posOptions = {timeout:3000,enableHighAccuracy: true};
$cordovaGeolocation
.getCurrentPosition(posOptions)
.then(function (position) {
var geo = {};
geo.location_latitude=position.coords.latitude;
geo.location_longitude=position.coords.longitude;
Service.CurrentService(geo).success(function(data){
console.log(new Date());
console.log("cur loc: "+JSON.stringify(data));
}).error(function(data){
console.log("error: "+JSON.stringify(data));
})
}, function(err) {
console.log("Error: "+err);
// here Position Error Occurs
// error
});
},60000);
}
})];
或者您可以更改:
$http({
url : 'yourserverURL',
method: 'POST',
data: obj,
header:{'Content-Type':'application/json; charset=utf-8'}
}).success(function (data,status,headers,config){
console.log(new Date());
console.log("cur loc: "+JSON.stringify(data));
}).error(function (data,status,headers,config){});
反而
Service.CurrentService(geo).success(function(data){
console.log(new Date());
console.log("cur loc: "+JSON.stringify(data));
}).error(function(data){
console.log("error: "+JSON.stringify(data));
})
**您还可以尝试将其放置在控制器中