cowbell / cordova-plugin-geofence

Geofencing plugin for cordova
Apache License 2.0
264 stars 318 forks source link

not found in Ionic 3 #232

Open marcelofb opened 7 years ago

marcelofb commented 7 years ago

not found the method "onTransitionReceived" in Ionic 3.3.0

shaneparsons commented 7 years ago

try calling it like this:

this.geofence.onTransitionReceived().subscribe((data) => {
  console.log(data);
});
marcelofb commented 7 years ago

is exactly my code this.geofence.onTransitionReceived().subscribe(resp => { //CODE });

shaneparsons commented 7 years ago

Your missing an extra set of braces around resp... Not sure if it makes a difference but it's worth a try. I had it working yesterday with my code.

marcelofb commented 7 years ago

Same problem. What versions do you use?

shaneparsons commented 7 years ago

I set up the project according to this. My environment is basically the latest version of everything.

marcelofb commented 7 years ago

this is my code:

constructor(public navCtrl: NavController, private geolocation: Geolocation,
  private geofence: Geofence, private sms: SMS, private platform: Platform) {

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

      geofence.initialize().then(
        () => console.log('Geofence Plugin Ready'),
        (err) => console.log(err)
      );
    })

  }

  setGeofence() {
    this.geolocation.getCurrentPosition({
      enableHighAccuracy: true
    }).then((resp) => {

      var longitude = resp.coords.longitude;
      var latitude = resp.coords.latitude;

      let fence = {
        id: "id",
        latitude: latitude,
        longitude: longitude,
        radius: 1,
        transitionType: 2
      }

      this.geofence.addOrUpdate(fence).then(
        () => this.success = true,
        (err) => this.error = "Failed to add or update the fence."
      );

      this.geofence.onTransitionReceived().subscribe(resp => {
        //CODE
      });

    }).catch((error) => {
      this.error = error;
    })

  }
shaneparsons commented 7 years ago

Where are you calling setGeofence()?

Here's my code:

import { Injectable } from '@angular/core';
import { Events, Platform } from 'ionic-angular';
import { Geofence } from '@ionic-native/geofence';

@Injectable()

export class GeofenceProvider {

  constructor(public geofence: Geofence, public events: Events, public platform: Platform) {

    platform.ready().then(() => {
      if (this.platform.is('cordova')) {

        geofence.initialize().then(
          () => {
            console.log('Geofence Plugin Ready');
            this.addGeofences();
          },
          (err) => console.log(err)
        )

      }
    });

  }

  addGeofences() {
    // options describing geofence
    let fences = [
      {
        id: 'beerstore',
        latitude: 45.349094,
        longitude: -80.031466,
        radius: 20,
        transitionType: 1,
        notification: {
          id: 1,
          title: 'Beer Store',
          text: 'You are near the Beer Store.',
          openAppOnClick: true
        }
      },
      {
        id: 'wellingtons',
        latitude: 45.349094,
        longitude: -80.031466,
        radius: 20,
        transitionType: 1,
        notification: {
          id: 2,
          title: 'Wellingtons',
          text: 'You are near Wellingtons.',
          openAppOnClick: true
        }
      }
    ]

    this.geofence.addOrUpdate(fences).then(
      () => console.log('Geofences added'),
      (err) => console.log('Geofences failed to add')
    );

    this.geofence.onTransitionReceived().subscribe((data) => {
      console.log(data);
      this.events.publish('transition:recieved');
    });

  }

}
marcelofb commented 7 years ago

In the HTML file <button ion-button large outline block (click)="setGeofence()" id="cta" color="primary">Set Geofence

seed880505 commented 7 years ago

This problem should be issued to https://github.com/ionic-team/ionic-native Update your ionic-native to latest version in package.json : "@ionic-native/core": "3.12.1", "@ionic-native/geofence": "3.12.1"