应用程序的目标:在应用程序位于前景和背景中时,获取每次移动的地理位置并记录日志位置。
我已经尝试了很多代码和组合,但是我无法使其正常工作(从现在起2天...)。
经典的地理位置(getCurrentPosition)可以正常工作,但是当我们关闭应用程序时,背景地理位置已启动,但没有任何反应...函数“callbackFn”从不触发。
我正在使用xcode>功能音频和位置激活的后台 Activity 在IOS上进行测试。我还使用了插件中提供的jQuery示例示例,因此我看到它可以正常工作,但不适用于ionic / angularjs。
这是当前处理后台的控制器:
.controller('TestCtrl', function($scope, $timeout, $cordovaBackgroundGeolocation, $ionicPlatform, $window)
{
$scope.lat_geo = "loading lat...";
$scope.long_geo = "loading long...";
//-- Geolocal launch
var options = {
enableHighAccuracy : false,
desiredAccuracy: 0,
stationaryRadius: 1,
distanceFilter: 5,
notificationTitle: 'Background tracking', // <-- android only, customize the title of the notification
notificationText: 'ENABLED', // <-- android only, customize the text of the notification
activityType: 'AutomotiveNavigation',
debug: true, // <-- enable this hear sounds for background-geolocation life-cycle.
stopOnTerminate: false // <-- enable this to clear background location settings when the app terminates
};
$ionicPlatform.ready(function()
{
console.log("[IONIC PLATFORM IS NOW READY]");
//-- First launch a basic geolocalisation to get user acceptance of geosharing ;)
navigator.geolocation.getCurrentPosition(function(location) {
console.log('[GEOLOCAL JS1] Location from Phonegap');
},
function (error){
console.log('[GEOLOCAL JS1] error with GPS: error.code: ' + error.code + ' Message: ' + error.message);
},options);
//-- test adaptation depuis l'app jquery
var callbackFn = function(location) {
console.log('[BackgroundGeoLocation] Update callback: ' + location.latitude + ',' + location.longitude);
};
var failureFn = function(error) {
console.log('[BackgroundGeoLocation] Error: '+error);
};
$cordovaBackgroundGeolocation.configure(callbackFn, failureFn, options);
// Turn ON the background-geolocation system. The user will be tracked whenever they suspend the app.
$cordovaBackgroundGeolocation.start();
//-- Just a timeout to retreive long / lat
$timeout(function()
{
navigator.geolocation.getCurrentPosition(function(location)
{
console.log('[GEOLOCAL JS3] Location from Phonegap');
startPos = location;
$scope.$apply(function () {
$scope.lat_geo = startPos.coords.latitude;
$scope.long_geo = startPos.coords.longitude;
});
console.log("[GEOLOCAL BASIC] OK this time :)");
},
function (error){
console.log('[GEOLOCAL JS3] error with GPS: error.code: ' + error.code + ' Message: ' + error.message);
},options);
}, 3000);
});
//-- End Geolocal
})
我已经将所有代码(完整的ionic应用程序入门)放在github上:https://github.com/Jeff86/ionic_ngcordova_backgroundgeo_test/tree/master
最佳答案
我读了这篇文章http://ngcordova.com/docs/plugins/backgroundGeolocation/
我发现您应该将代码放入 document.addEventListener("deviceready", function () { ... });
你有什么解决办法吗?