Android: Build Polygonal Shape Geofence
B

2

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,

Blithesome answered 6/8, 2015 at 6:8 Comment(0)
T
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

Tented answered 21/8, 2015 at 6:16 Comment(2)
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 earlierPass
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.Songful
D
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?

Drachma answered 6/8, 2015 at 6:32 Comment(4)
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?Blithesome
@Blithesome - 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.Mystical
@Rhonage No I haven't foundBlithesome
"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.Songful

© 2022 - 2024 — McMap. All rights reserved.