Cordova-plugin-geofence onTransitionReceived()方法不起作用

时间:2016-09-16 15:02:18

标签: cordova angular typescript cordova-plugins ionic2

我正在我的项目中使用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);
      });
    }
  });
  

HomePage.ts

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

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

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

怎么了?谢谢!

1 个答案:

答案 0 :(得分:1)

文档不正确,您需要订阅:

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

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

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

如果您想使用功能" getWatched",您需要快速支持..只需添加:

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

的Config.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;
    });

这些设置对我有用。