离子中的多重地理围栏

Multiple geofencing in ionic

在我的离子项目中,我使用了地理围栏。 当我在该地区打开应用程序时,通知正在显示。 怎么才能在越过地理围栏边界时才看到通知呢

并且我使用了3个栅栏(12Km.300m,50m)。当在50m的范围内应用程序显示三个通知(三个栅栏的通知)。 当我处于50m范围条件范围内时,如何显示唯一一个通知。

在app.component.ts

 constructor(public platform: Platform,
              public statusBar: StatusBar,
             ...........) {

      this.addGeofence();
  }


private addGeofence() {
    let fence = [{
      id: '69ca1b88-6fbe-4e80-add-sd-4d3748-far',
      latitude:       8.556498,
      longitude:      76.881820,
      radius:         12000,
      transitionType: 1,
        notification: {
          id:             111111111,
          title:          'Welcome ',
          text:           '12km range.',
          openAppOnClick: true
        }
    },
      {
        id: '69ca1b88-6fbe-4e80-add-sd-4d3748-closevicinity',
        latitude:       8.556498,
        longitude:      76.881820,
        radius:         300,
        transitionType: 1,
        notification: {
          id:             222222222,
          title:          'Welcome',
          text:           '300m range',
          openAppOnClick: true
        }
      },
      {
        id: '69ca1b88-6fbe-4e80-add-sd-4d3748-near',
        latitude:       8.556498,
        longitude:      76.881820,
        radius:         50,
        transitionType: 1,
        notification: {
          id:             333333333,
          title:          'Welcome',
          text:           '50m range',
          openAppOnClick: true
        }
      },]

我的建议:可以使用ionic的geolocation插件获取当前位置。要么使用它来每分钟检查一次位置,要么使用后台地理定位插件在后台模式下执行此操作。对于您获得的每个位置更新,请检查您是否在地理围栏的半径之一内。您可以使用像 turf 这样的库来做数学运算。

如果您只想在越过边界时收到通知,请将您上次进入的地理围栏保存到存储中,每次启动应用程序时检查地理围栏是否发生变化。

注意位置订阅和后台模式,因为它可能会消耗大量电池。