Improving Google’s Geofencing

by / Wednesday, 03 July 2013 / Published in Blog, Technical

Background

Lately I’ve decided to try out the new Location APIs included in Google’s Play Services. Specifically, I wanted to see how the new Geofencing feature looks like and what can I do with it.

Since聽necessity is the mother of invention, it seemed appropriate to try it on my home’s parking gate. My gate has a modem connected to it, and in order to open it you have to call a certain phone number. The modem recognizes the caller ID, verifies that the caller is authorized to open the gate, and if so – it just opens it (without answering the call, of course).

A-HA! So why not define a geofence around the gate, and let my phone call it automatically as soon as I arrive?

The Problem

So I set up and wrote an app (actually, a service) which does exactly that. But when I tested it, I encountered a problem – the fence’s trigger arrived quite late. Almost every time I got at the gate, it took some time till the fence recognized I’m actually there and opened it. I was left hanging out to dry, till the Location service would kindly decide to refresh its data.
So I needed a way to force the Location APIs to use the GPS more frequently when I’m near the fence.

The solution

Ok, so let’s force it! I defined a second geofence, with a bigger radius than the first one. Upon getting a trigger for the new outer geofence, I registered my service to receive location updates. Whenever I got such an update – I actually did nothing with it! However, the side effect was that the original geofence enjoyed a frequent refresh in GPS data, resulting in a much more accurate location.

Conclution

Thinking of this solution, I realized that when registering a goefence, I expect the API to also get an聽“accuracy” parameter from the app. Then, the Location service can decide by itself when to use the GPS – for example, by using the activity recognition mechanism: if I’m walking, it would start the GPS only when I get within 1000ft from the fence, and if I’m driving, it would start it at a much farther distance.

 

Notes
  • When entering the inner geofence – I immediately stop the location updates.
  • Also, if the user has entered the outer geofence, after X minutes without entering the inner one, I stop the updates. I don’t want to drain the battery.
  • In my experience, after getting home, I’ve noticed that from time to time the device thinks it exited the inner geofence, and then back in it. So – if the user exited the inner one, but didn’t exited the second one after that, I will not open the gate if he is back in the inner one.

 

4 Responses to “Improving Google’s Geofencing”

  1. Flo says : Reply

    Hi, well done!

    i just got the same problem.
    Is it really just a side effect that the location updates would be used by the geofence listener? That would be great.

    Is your solution like this:
    If bigger Geofence is triggering switch to location updates and use onLocationChanged(Location location) listener. Then use the location.distanceTo() Method to check if the radius of geofence is passed?

    Could you give me some sample code of your solution?

    Thanks,

    Flo

    Thanks

    • Yair says : Reply

      Hi Flo,
      You got the basic idea – the bigger geofence triggers location updates, and the onLocationChanged method does nothing. However – I don’t need to check the location I got in that method! The side effect of the location updates is that the device starts using the GPS for better location results. This will cause the small geofence to work better, since it now can enjoy the GPS capabilities (behind the scene, by the play services).
      In other words, I still using the inner geofence as before – it simply works better now.

  2. Flo says : Reply

    Ok, i will try that out 馃檪 Thanks for the quick answer.

    It will work great for entering a geofence:
    Entered big fence -> UpdateLocation for better accuracy -> Entered small fence

    But what is about leaving a geofence?
    If i am in the small fence (my home for example) i don’t want to do UpdateLocation all the time… So i have to disable it if i am in my small fence… ….

    Any ideas?
    Maybe with activity recognition … if got “still” with a confidence of >50 i can disable and if got on_foot or so enable it…

    Anyway it is a good workaround so far. Thanks for Blogging this 馃檪

    • Yair says : Reply

      Well – when the user enters the inner geofence, I stop the location updates – I don’t need it any more. You are right that when the user will leave the inner geofence – We’re back with the same problem. Your solution of using the activity recognition sounds right. It would be nice if my app would know when to open the gate when leaving home…

Leave a Reply

TOP