7

According to the API documentation, only Circular shape Geofences allowed:

https://developers.google.com/android/reference/com/google/android/gms/location/Geofence.Builder

And it's looks for example: https://developer.android.com/training/location/geofencing.html

But I have 4 locations, representing 4 corners of rectangle, and I want my Geofence to be that rectangle.

I want to avoid solution of building my custom location monitoring service extending the functionality of monitoring Geofences, because I think this kind of services are CPU & power consuming...

Thanks,

michael
  • 3,835
  • 14
  • 53
  • 90

2 Answers2

11

You have to do it by yourself (however I think it's rather senseless - geofences/location services are inaccurate enough to not even be able to alert about circles, don't even think about some other concrete shape).

Create smallest circular geofence which contains your polygon and if triggered just check if you are in the polygon inside with Google Maps Android API utility library.

In other words check if you are in white area not in grey:

enter image description here

Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103
  • 7
    This wouldn't work. If you enter the grey area and do the check it would return false. Then later you enter the white area and nothing happens because the ENTER callback was already processed earlier – Tim Nov 01 '16 at 09:44
  • 3
    You didn't get my response properly, so let me explain again: after trigger from grey circular geofence you have to poll for position and constantly check if you are in white square yet or not. – Marian Paździoch Dec 29 '16 at 11:55
0

The Android Geofencing API only provides support for Circular Geofences. If you want to monitor Polygon shaped geofences then you have to implement it your self.

One solution would be something like this:

Get the current location update using Location API.

Now you have polygon shaped geofence. You can use ray-casting Algorithm to check if a point is inside the Closed Polygon or not?

Sunny Shah
  • 918
  • 1
  • 8
  • 8
  • Thanks, The point is I need to build Android service module that's always run in the background (even if the app is closed) and once in a while wakes up and checks if the current user location is within the Geofence (using the algorithm you suggest). Is it recommended to implement such services? It's not to much consuming CPU and power? – michael Aug 06 '15 at 06:44
  • 1
    @michael - I'm attempting to do the same thing as you. Did you find a solution? Foreground services are all good, but everything changes with Android O. Thanks. –  Jun 24 '18 at 09:58
  • @Rhonage No I haven't found – michael Jun 24 '18 at 20:44
  • "Get the current location update using Location API." this stand in contradiction to geofence idea as geofence is all about NOT getting current location by yourself. – Marian Paździoch Feb 06 '19 at 10:49