Cordova Geofence Plugin


#1

Hey,

I have a problem and hope some can give me some insight on what I’m doing wrong.

I start with a blank Ionic tabs app and then add the Cordova geofence plugin (https://github.com/cowbell/cordova-plugin-geofence) via cordova plugin add cordova-plugin-geofence
Inside my app.js I try to initialize it via

$ionicPlatform.ready(function () {
  window.geofence.initialize();
}

But this doesn’t work. In my browser console it says: “window.geofence is undefined”.

I’ve seen the ionic sample app but found it not really helpful as the app is quite complex already. I want to recreate this step by step in order to improve my skills.

Any help would be appreciated.

Thanks,
Florian


#2

Anyone able to help me and shine some light on this? Thank you.


#3

Of course, it’s not working.

Use:

document.addEventListener('deviceready', function () {
    // window.geofence is now available
    window.geofence.initialize();
}, false);

instead of:

$ionicPlatform.ready(function () {
  window.geofence.initialize();
}

ionic.Platform.ready is not Cordova deviceready.


#4

Thank you for your help. But this didn’t work.

I have tried the following… but still I get the error that window.geofence is undefined… I don’t think it has something to do with how the method is called. Even with the 10 seconds timeout I can catch the console output but still get the undefined error.

.run(function($ionicPlatform) {

  document.addEventListener('deviceready', function () {
      // window.geofence is now available
      window.geofence.initialize();
  }, false);

  setTimeout(function() {
    console.log('test1');
    window.geofence.initialize();
    console.log('test');
    console.log(window.geofence);
  }, 10000);

  $ionicPlatform.ready(function() {
    …
  });
})

#5

I’m getting a bit desperate here… has anybody an idea, how to get this plugin working?


#6

Maybe it only works on a device, not under “ionic serve”. I’m also going to evaluate this geofence stuff.


#7

I’m not sure… I tried it with the Ionic view app. But I also tried their Iionic example app and it also didn’t work.


#8

Ionic View doesn’t contain all Cordova plugins so I can imagine it doesn’t work. I think you need to do “ionic build” or “ionic run” with a real device.


#9

Used Ionic run with the sample app but still no luck… it stops at “Obtaining location”.


#10

Okay, I did some more diggiing and experience really strange behaviour:

I tested both the plugins https://github.com/cowbell/cordova-plugin-geofence and https://github.com/christocracy/cordova-plugin-background-geolocation

I can’t get them to work them when I’m using an Ionic workflow but I can get them to work using a Cordova workflow

What I Do:

  • Create an Ionic app with $ionic start geotest
  • Install one of the plugins as described on their github repo
  • when I try to initialize them, I get the error described in my first post, that the object is undefined

When using a Cordova workflow (cordova create geotest and installing one the pf the plugins as described on their repo I can access the object and everything seems to work fine).

Can somebody tell me what I’m doing wrong here? I never experienced this behaviour before with other plugins.


#11

Update to the latest cordova and ionic

sudo npm install -g ionic cordova