cowbell / cordova-plugin-geofence

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

Inconsistencies with onTransitionReceived() #293

Open cathrinerathje opened 5 years ago

cathrinerathje commented 5 years ago

I'm creating an Ionic app in which I use geofences to read information aloud about sights the user passes, and also to read guidance instructions (like Google Maps) for the user along the route.

Therefore I have two types of geofences - sights with radius 100m and directions with radius 50m. They are being added in the exact same way, and the addOrUpdate() method is succesful in both cases. However, only the directions geofences are being registered by onTransitionReceived().

The directions geofences are being created from an array of directions received from a Google DirectionsService response. The sights geofences are being created from an array of sights retrieved from a previous page in the app. The only way I can make it work for the sights geofences is if I first format the properties I need into an array and then create and add the fences from it. This seems very strange to me since the added geofences look identical when printed to the console.

Here is how I do it:

In constructor:

geofence.removeAll().then(()=>{ console.log('all geofences removed'), (err)=>{ console.log(err); } });

geofence.initialize().then(()=>{ console.log('geofence plugin ready'), (err)=>{ console.log(err); } });

Adding geofences in .ts file:

` private addSightGeofences() { let fences = [];

this.sights.map((sight, index) => {
  let fence = {
    id: 's' + index,
    latitude: sight.lat,
    longitude: sight.lng,
    radius: 100,
    transitionType: 1,
    notification: {
      id: index,
      title: 'You crossed a sight',
      text: 'You just arrived at '+ sight.name,
      openAppOnClick: true,
      data: {
        isSight: true,
        sightName: sight.name,
        originalName: sight.originalName,
        text: sight.longDescription
      }
    }
  }
  fences.push(fence);
});

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

} `

`addDirectionsGeofences() { let fences = [];

this.directions.map((step, index) => {
  let fence = {
    id: 'd' + index,
    latitude: step.lat,
    longitude: step.lng,
    radius: 50,
    transitionType: 1,
    notification: {
      id: index,
      title: 'You reached a direction',
      text: 'You just arrived at '+ step.instructions,
      openAppOnClick: true,
      data: {
        isSight: false,
        instructions: step.instructions,
        value: step.value
      }
    }
  }
  fences.push(fence);
});

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

}`

Then the call to onTransitionReceived() also in .ts file:

addGeofenceTransitions() { this.geofence.onTransitionReceived().subscribe((res) => { console.log(res); res.forEach((f) => { console.log(f); if (f.notification.data.isSight == true) { console.log('sight geofence reached'); this.sightSpeech(f.notification.data); this.updateDirectionsGeofences(); } else { console.log('directions geofence reached'); this.directionsSpeech(f.notification.data.instructions, f.notification.data.value); } }); }); console.log('geofence transitions added'); }