【问题标题】:Cordova-plugin-geofence onTransitionReceived() method not workingCordova-plugin-geofence onTransitionReceived() 方法不起作用
【发布时间】:2017-01-24 20:52:39
【问题描述】:

我在我的项目中使用 Ionic2 和 Angular2。当我安装 cordova-plugin-geofence (https://github.com/cowbell/cordova-plugin-geofence) 时。地理围栏配置如下:

App.ts

platform.ready().then(() => {

  if (window.geofence === undefined) {
    console.log('Geofence Plugin not found');
  }else{
    console.log('Geofence plugin found');
    window.geofence.initialize();
    const geofence = this.geofenceService.create({
      longitude: -8.404015,
      latitude:  43.339147,
    });

  this.geofenceService.addOrUpdate(geofence).then(function () {
      console.log('Geofence successfully added');
  }, function (reason) {
      console.log('Adding geofence failed', reason);
      });
    }
  });

主页.ts

window.geofence.onTransitionReceived = function (geofences) {
  geofences.forEach(function (geo) {
    console.log('Geofence transition detected', geo);
  });
}

服务 geofence.service.ts 它被配置为 tsubik 的 ionic2 geofence 示例:https://github.com/tsubik/ionic2-geofence/blob/master/app/services/geofence-service.ts

在所有地理围栏配置过程中,控制台上都不会出现错误,但是当地理围栏转换发生时,方法 window.geofence.onTransitionReceived() 不会记录任何内容。

怎么了?谢谢!

【问题讨论】:

  • 你必须定义一个半径和一个过渡类型。不要使半径接近,取1000或3000m之类的东西。
  • @Samuel Fraga Mateos,你收到通知了吗?即使我在这个插件上也面临问题。
  • @Joerg 我在默认地理围栏上定义了半径和过渡类型。当我创建一个新的地理围栏时,我只覆盖纬度和经度参数。
  • @Aish123 我根本没有收到通知。

标签: cordova angular typescript cordova-plugins ionic2


【解决方案1】:

文档有误,需要订阅:

Geofence.onTransitionReceived().subscribe( res =>{

          res.forEach(function(geo) {
            console.log(geo);
          });

        },
        (err) => console.log(err),
        ()    => console.log("done !")
    );

如果你想使用函数“getWatched”,你需要有swift支持..只需添加:

cordova 插件添加 cordova-plugin-add-swift-support --save

配置.xml

  <preference name="UseLegacySwiftLanguageVersion" value="true"/>
  <plugin name="cordova-plugin-add-swift-support" spec="~1.6.2"/>

如果您需要当前正在监控的地理围栏数组:

   Geofence.getWatched().then((geofencesJson) => {

          const geofences = JSON.parse(geofencesJson);

      this.sendLog("wathced ");
      this.sendLog(geofencesJson);

          return geofences;
    });

这些设置对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    相关资源
    最近更新 更多