我是AngularJS和Ionic的新手 . 我正在构建一个Ionic(版本1)的应用程序,它将在后台收集设备的GPS位置 . 我正在尝试使用cordova-plugin-mauron85-background-geolocation插件来实现此目的 . 但我收到一个错误 Uncaught ReferenceError: backgroundGeolocation is not defined.
我的app.js.
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'firebase', 'ngMaterial'])
.factory('BackgroundGeolocationService', ['$q', '$http', function ($q, $http) {
var callbackFn = function(location) {
$http({
//request options to send data to server
});
backgroundGeolocation.finish();
},
failureFn = function(error) {
console.log('BackgroundGeoLocation error ' + JSON.stringify(error));
},
//Enable background geolocation
start = function () {
//save settings (background tracking is enabled) in local storage
window.localStorage.setItem('bgGPS', 1);
//...........ERROR IS ON THE FOLLOWING LINE.........
backgroundGeolocation.configure(callbackFn, failureFn, {
desiredAccuracy: 10,
stationaryRadius: 20,
distanceFilter: 30,
locationService: 'ANDROID_DISTANCE_FILTER',
debug: false,
stopOnTerminate: false
});
backgroundGeolocation.start();
};
return {
start: start,
// Initialize service and enable background geolocation by default
init: function () {
var bgGPS = window.localStorage.getItem('bgGPS');
if (bgGPS == 1 || bgGPS == null) {
start();
}
},
// Stop data tracking
stop: function () {
window.localStorage.setItem('bgGPS', 0);
backgroundGeolocation.stop();
}
}
}])
.run(function($ionicPlatform, BackgroundGeolocationService) {
$ionicPlatform.ready(function() {
BackgroundGeolocationService.init();
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
});
})
我在这里错过了什么?
2 回答
该插件是exposed via the backgroundGeolocation global namespace但您将其引用为
backgroundGeoLocation
.注意区别:你使用的是资本L.Javascript是区分大小写的,所以你需要小心大小写 .
我以前遇到过Ionic / Cordova插件的问题 . 对我来说,解决方案是执行以下操作:
ionic platform rm android
ionic platform add android
然后,您可以运行 ionic run android --device 并等待应用程序在您的Android设备上运行 . 加载后,打开Chrome浏览器并输入Chome:// Inspect into the URL bar . 找到您的App Webview并单击Inspect这将打开常用的Chrome Dev Tools,您可以在其中找到任何控制台日志,缺少插件,错误等 .