Skip to content

Instantly share code, notes, and snippets.

@thorwebdev
Last active August 29, 2015 14:10
Show Gist options
  • Save thorwebdev/5ea0eae9b7e7399af065 to your computer and use it in GitHub Desktop.
Save thorwebdev/5ea0eae9b7e7399af065 to your computer and use it in GitHub Desktop.
We need an endpoint to test any latitude-longitude-pair against all our fences. This is the actual geofencing part. We want to be able to know, if the point falls into any of our fences and if so, we want to get back the ids of the fences the point is in. For this we first need to retrieve our index from Memcache. We then query the index with th…
@ApiMethod(name = "point", httpMethod = "get", path = "point")
public ArrayList < MyFence > queryPoint(@Named("group") String group, @Named("lng") double lng, @Named("lat") double lat) {
ArrayList < MyFence > fences = new ArrayList < MyFence > ();
//Get the Index from Memcache.
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
GeometryFactory gf = new GeometryFactory();
STRtree index = (STRtree) syncCache.get(group); // read from cache
if (index != null) {
Coordinate coord = new Coordinate(lng, lat);
Point point = gf.createPoint(coord);
List < MyPolygon > items = index.query(point.getEnvelopeInternal());
if (!items.isEmpty()) {
for (MyPolygon poly: items) {
if (poly.contains(point)) {
long id = poly.getID();
MyFence newFence = new MyFence();
newFence.setId(id);
fences.add(newFence);
}
}
}
} else {
try {
MyIndex.buildIndex(group);
} catch (IOException e) {
e.printStackTrace();
}
}
return fences;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment