Ionic应用程序背景中的地理位置报告 [英] Geolocation reporting in Ionic app Background
问题描述
即使应用程序在后台处于休眠状态,我也打算让用户进行地理位置定位,并将其存储在数据库中.我正在使用katzer的Cordova背景插件,当我尝试访问 backgroundMode.onactivate
函数中的 navigator.geolocation.getCurrentPosition
时,什么也没有发生,而当我尝试传递硬编码值api时,数据存储在数据库中
I intend to get users geolocation even when the app sits dormant in the background and store the same in the database.
I'm using katzer's Cordova Background Plug-in,
When I try to access navigator.geolocation.getCurrentPosition
inside backgroundMode.onactivate
function, nothing happens, Whereas when I try passing hard coded values api is called, data is stored in database.
以下是我的代码
document.addEventListener('deviceready', function() {
// Android customization
cordova.plugins.backgroundMode.setDefaults({
text: 'Doing heavy tasks.'
});
// Enable background mode
cordova.plugins.backgroundMode.enable();
// Called when background mode has been activated
cordova.plugins.backgroundMode.onactivate = function() {
console.log('inside background')
a();
}
var a = function() {
console.log('a called')
navigator.geolocation.getCurrentPosition(function(pos) {
console.log('inside navigate');
var data = {
Lati: '123456',
Longi: '132456',
//LoginID: JSON.parse(window.localStorage.getItem('LoginId'))
EmpCode: localStorage.getItem('LoginId')
};
$http.post("https://app.sbismart.com/bo/ContactManagerApi/UpdateEmployeeLoc", data).success(function(rsdata, status) {
console.log('inside rsdata');
console.log(data.Lati + "," + data.Longi);
})
}, function(error) {
alert('Unable to get location: ' + error.message);
});
}
}, false);
推荐答案
cordova.plugins.backgroundMode.onfailure = function(errorCode) {
console.log(errorCode)
};`
并检查其失败原因....然后,您再次需要在后台的超时功能中运行locationService函数,以获取有关位置的更新并从先前获得的位置检查位置.像这样...
and check as to why is it failing....then again u need to run the locationService function in a timeout function in the background to get updated about the location and check the location from previously got location. Something like this...
cordova.plugins.backgroundMode.onactivate = function () {
setTimeout(function () {
a();
}, 5000);
}
希望这会有所帮助.
这篇关于Ionic应用程序背景中的地理位置报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!