首页 > 解决方案 > BackgroundGeolocation 与 Angular 9 和 Cordova

问题描述

我尝试将 @mauron85/cordova-plugin-background-geolocation 插件添加到我的 Angular9 + Cordova 项目(不是 Ionic)中。我安装了 Cordova 插件和 NPM 插件。我将插件添加到我的项目中

import {BackgroundGeolocationPlugin} from '@mauron85/cordova-plugin-background-geolocation';

现在我只是试图让它与我的 Main.ts 中的以下内容一起工作

const bootstrap = () => {
  console.log("Platform is Android or IOS")
      var backgroundGeolocation: BackgroundGeolocationPlugin;
      backgroundGeolocation.configure({
      locationProvider: backgroundGeolocation.DISTANCE_FILTER_PROVIDER,
      desiredAccuracy: backgroundGeolocation.HIGH_ACCURACY,
      stationaryRadius: 5,
      distanceFilter: 5,
      notificationTitle: 'SOF-Background Location',
      notificationText: 'enabled',
      debug: true,
      interval: 60000,
      fastestInterval: 10000,
      activitiesInterval: 10000,
      url: 'http://192.168.81.15:3000/location',
      // customize post properties
      postTemplate: {
        lat: '@latitude',
        lon: '@longitude',
      }
    });

    backgroundGeolocation.on('location', function(location) {
      console.log(location)
    });

    backgroundGeolocation.on('error', function(error) {
      console.log('[ERROR] BackgroundGeolocation error:', error.code, error.message);
    });

    backgroundGeolocation.on('start', function() {
      console.log('[INFO] BackgroundGeolocation service has been started');
    });

    backgroundGeolocation.on('stop', function() {
      console.log('[INFO] BackgroundGeolocation service has been stopped');
    });

    backgroundGeolocation.on('authorization', function(status) {
      console.log('[INFO] BackgroundGeolocation authorization status: ' + status);
      if (status !== backgroundGeolocation.AUTHORIZED) {
        // we need to set delay or otherwise alert may not be shown
        setTimeout(function() {
          var showSettings = confirm('App requires location tracking permission. Would you like to open app settings?');
          if (showSettings) {
            return backgroundGeolocation.showAppSettings();
          }
        }, 1000);
      }
    });

    backgroundGeolocation.on('background', function() {
      console.log('[INFO] App is in background');
      // you can also reconfigure service (changes will be applied immediately)
      backgroundGeolocation.configure({ debug: true });
    });

    backgroundGeolocation.on('foreground', function() {
      console.log('[INFO] App is in foreground');
      backgroundGeolocation.configure({ debug: false });
    });

    backgroundGeolocation.on('abort_requested', function() {
      console.log('[INFO] Server responded with 285 Updates Not Required');

      // Here we can decide whether we want stop the updates or not.
      // If you've configured the server to return 285, then it means the server does not require further update.
      // So the normal thing to do here would be to `BackgroundGeolocation.stop()`.
      // But you might be counting on it to receive location updates in the UI, so you could just reconfigure and set `url` to null.
    });

    backgroundGeolocation.on('http_authorization', () => {
      console.log('[INFO] App needs to authorize the http requests');
    });

    backgroundGeolocation.checkStatus(function(status) {
      console.log('[INFO] BackgroundGeolocation service is running', status.isRunning);
      console.log('[INFO] BackgroundGeolocation services enabled', status.locationServicesEnabled);
      console.log('[INFO] BackgroundGeolocation auth status: ' + status.authorization);

      // you don't need to check status before start (this is just the example)
      if (!status.isRunning) {
        backgroundGeolocation.start(); //triggers start on start event
      }
    });

    // you can also just start without checking for status
    // BackgroundGeolocation.start();

    // Don't forget to remove listeners at some point!
    // BackgroundGeolocation.removeAllListeners();
  platformBrowserDynamic().bootstrapModule(AppModule);
};

if (typeof window["cordova"] !== "undefined") {
  document.addEventListener(
    "deviceready",
    () => {
      bootstrap();
    },
    false
  );
} else {
  platformBrowserDynamic().bootstrapModule(AppModule);
}

但每次我尝试启动 Android 应用程序时,都会出现以下控制台错误:

Uncaught TypeError: Cannot read property 'configure' of undefined
    at bootstrap (main.ts:15)

标签: angularcordova

解决方案


我知道这可能不是你想要的,但是这个插件不能正常工作。目前有一个用于后台地理定位的工作插件,它运行完美,并且每天进行测试和维护,但它不是免费的。在我们的团队中,我们决定购买它,因为我们需要这个功能。

https://github.com/transistorsoft/cordova-background-geolocation-lt


推荐阅读