未捕获的ReferenceError:backgroundGeolocation未定义cordova-plugin-mauron85-background-geolocation

时间:2017-03-20 06:33:43

标签: angularjs cordova ionic-framework cordova-plugins

我是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 个答案:

答案 0 :(得分:3)

该插件为exposed via the backgroundGeolocation global namespace,但您将其引用为backgroundGeoLocation

注意区别:你使用的是资本L. Javascript区分大小写,所以你需要小心处理。

答案 1 :(得分:1)

之前我遇到过Ionic / Cordova插件的问题。对我来说,解决方案是执行以下操作:

离子平台rm android

离子平台添加android

然后你可以运行离子运行android --device 并等待应用程序在你的Android设备上运行。加载后,打开Chrome浏览器并在 Chome:// Inspect 中输入网址栏。找到您的App Webview并单击 Inspect 这将打开常用的Chrome Dev Tools,您可以在其中找到任何控制台日志,缺少插件,错误等。