Tuesday, March 18, 2014

Point Geo Fencing - Sample Code

Point Geo Fencing is easier than polygon Geo Fencing.

As described in my first blog (read it here), you just have to calculate the distance between the point and your current location, and compare it with the radius of your point (circle).

First we need an object for representing our Point Geo Fence:


When your radius is defined in meters, you will need the Haversine formula.  This formula will calculate the distance between two points (in meters) while taking into account the earth curvation:


Now, we can calculate if we're inside the Geo Fence or not:


X and Y are your current longitude and -latitude.
The method will return true, when we're inside the Geo Fence and false when we're not.

More information can be found here:
http://en.wikipedia.org/wiki/Haversine_formula
http://stefanbangels.blogspot.be/2012/12/for-several-years-now-i-have-been.html
http://stefanbangels.blogspot.nl/2013/10/geo-fencing-sample-code.html

2 comments:

Unknown said...

Have you removed the java sample code ? or you were referring to the algorithm mentioned in post

Anonymous said...

You should be able to view the code in the wayback machine:

https://web.archive.org/web/20180423014220/http://stefanbangels.blogspot.com/2014/03/point-geo-fencing-sample-code.html

https://web.archive.org/web/20180708164347/http://stefanbangels.blogspot.com/2013/10/geo-fencing-sample-code.html