我在带有angularjs和ionic的phonegap项目中,地理位置后台处理(https://github.com/christocracy/cordova-plugin-background-geolocation)出现问题。该过程运行良好,但有时无法停止。如果应用程序在前台运行,则一切正常。但是,当我启动后台服务,然后按设备(Samsung Galaxy S2)上的主页按钮时,该进程在后台运行->正确。现在,我重新打开该应用程序,并尝试停止后台进程,但是此时它不起作用。看来我没有正确的流程实例。这是我的代码:
var bgGeo;
angular.module('starter.services', [])
.factory('LocationService', function($http, $location){
function startBackgroundLocation() {
var gpsOptions = {
enableHighAccuracy : true,
timeout: 10000,
maximumAge: 5000
};
navigator.geolocation.getCurrentPosition(function(location) {
console.log('Location from Phonegap');
},
function (error){
alert('error with GPS: error.code: ' + error.code + ' Message: ' + error.message);
},gpsOptions);
bgGeo = window.plugins.backgroundGeoLocation;
/**
* This would be your own callback for Ajax-requests after POSTing background geolocation to your server.
*/
var yourAjaxCallback = function(response) {
////
// IMPORTANT: You must execute the #finish method here to inform the native plugin that you're finished,
// and the background-task may be completed. You must do this regardless if your HTTP request is successful or not.
// IF YOU DON'T, ios will CRASH YOUR APP for spending too much time in the background.
//
//
bgGeo.finish();
};
/**
* This callback will be executed every time a geolocation is recorded in the background.
*/
var callbackFn = function(location) {
alert('[js] BackgroundGeoLocation callback: ' + location.latitudue + ',' + location.longitude);
// Do your HTTP request here to POST location to your server.
//
//
// This is never called in Android
yourAjaxCallback.call(this);
};
var failureFn = function(error) {
alert('BackgroundGeoLocation error');
}
// BackgroundGeoLocation is highly configurable.
bgGeo.configure(callbackFn, failureFn, {
url: apiUrl + '/position/', // <-- only required for Android; ios allows javascript callbacks for your http
params: { // HTTP POST params sent to your server when persisting locations.
auth_token: localStorage.getItem('gcmToken')
},
desiredAccuracy: 10,
stationaryRadius: 20,
distanceFilter: 30,
debug: true // <-- enable this hear sounds for background-geolocation life-cycle.
});
// Turn ON the background-geolocation system. The user will be tracked whenever they suspend the app.
// wenn der Service bereits läuft, nicht mehr starten
bgGeo.start();
}
function stopBackgroundLocation(){
bgGeo.stop();
}
return {
start: function(){
init();
},
stop : function () {
stopBackgroundLocation();
}
}
}
在应用恢复后,当我调用LocationService.stop()
时,后台地理位置不会停止。有人知道什么地方出问题或我必须补充什么吗?谢谢。
@@ Aleks:也许您知道解决方案?
我现在使用此功能:
function stopService(){
var bgGeoService = window.plugins.backgroundGeoLocation
bgGeoService.stop()
}
而且似乎可行。但是还有另一种方法,请参见以下链接:https://github.com/christocracy/cordova-plugin-background-geolocation/issues/48
希望这会有所帮助。