如何在地理围栏中动态观察纬度和经度

时间:2017-05-29 11:13:24

标签: cordova ionic2 android-geofence ionic3 ionic-native

当我完成地理围栏时,我有应用程序,我能够完美地获得通知。

我的目标是在达到半径时动态获取通知。

现在当我添加geofence时,如果我需要再次获得另一个通知,则addorupdate函数只能正常运行一次,然后我想再次添加。

而不是那种方式,我可以看到我的纬度和经度背景,而不是一次又一次地添加。

这是我的代码

  lat 
  long

  constructor(public navCtrl: NavController, private geofence: Geofence, private geolocation: Geolocation) {

    this.geolocation.getCurrentPosition().then((resp) => {
    // resp.coords.latitude
    // resp.coords.longitude
    console.log(resp);
    console.log("invoking the geolocation success");
    this.lat = resp.coords.latitude;
    this.long = resp.coords.longitude;

    console.log(this.lat);
    console.log(this.long);

    this.addGeofence();
    }).catch((error) => {
      console.log('Error getting location', error);
    });
    console.log(this.lat);
    console.log(this.long);
    // initialize the plugin
    geofence.initialize().then(
      // resolved promise does not return a value
      (success) => {
        console.log(success);
        console.log('Geofence Plugin Ready')
      },
      (err) => {
        console.log(err);
        console.log(err)
      })
  }

  private addGeofence() {
    //options describing geofence
    let fence = {
      id: '69ca1b88-6fbe-4e80-a4d4-ff4d3748acdb', //any unique ID
      latitude:       this.lat, //center of geofence radius
      longitude:      this.long,
      radius:         50, //radius to edge of geofence in meters
      transitionType: 3, //see 'Transition Types' below
      notification: { //notification settings
          id:             1, //any unique ID
          title:          'You crossed bangalore', //notification title
          text:           'You just arrived to bangalore.', //notification body
          openAppOnClick: true //open app when notification is tapped
      }
    }
    this.geolocation.getCurrentPosition().then((resp) => {
    // resp.coords.latitude
    // resp.coords.longitude
    this.lat = resp.coords.latitude;
    this.long = resp.coords.longitude;

    }).catch((error) => {
      console.log('Error getting location', error);
    });
    console.log(fence);

    this.geofence.addOrUpdate(fence).then(
      (success) => {
        console.log(success);
        console.log('Geofence added')
      },
      (err) => {
        console.log(err);
        console.log('Geofence failed to add')
      }); 
  }

一旦我们添加地理围栏,我怎样才能每次动态获取通知?

0 个答案:

没有答案