cordova-plugin-geofence
cordova-plugin-geofence copied to clipboard
not found in Ionic 3
not found the method "onTransitionReceived" in Ionic 3.3.0
try calling it like this:
this.geofence.onTransitionReceived().subscribe((data) => {
console.log(data);
});
is exactly my code this.geofence.onTransitionReceived().subscribe(resp => { //CODE });
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.
Same problem. What versions do you use?
I set up the project according to this. My environment is basically the latest version of everything.
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;
})
}
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');
});
}
}
In the HTML file <button ion-button large outline block (click)="setGeofence()" id="cta" color="primary">Set Geofence
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"