geolocation - Android Geofencing (Polygon) -
how create polygon geofence multiple geo locations(long,lat values) . how track user entering geofence region or exiting region on android.
a geofence array of lat/long points form polygon. once have list of lat/long points, can use point-inside-polygon check see if location within polygon.
this code have used in own projects perform point-in-polygon checks large concave polygons (20k+ vertices):
public class polygontest { class latlng { double latitude; double longitude; latlng(double lat, double lon) { latitude = lat; longitude = lon; } } bool pointisinregion(double x, double y, latlng[] thepath) { int crossings = 0; latlng point = new latlng (x, y); int count = thepath.length; // each edge (var i=0; < count; i++) { var = thepath [i]; var j = + 1; if (j >= count) { j = 0; } var b = thepath [j]; if (raycrossessegment(point, a, b)) { crossings++; } } // odd number of crossings? return (crossings % 2 == 1); } bool raycrossessegment(latlng point, latlng a, latlng b) { var px = point.longitude; var py = point.latitude; var ax = a.longitude; var ay = a.latitude; var bx = b.longitude; var = b.latitude; if (ay > by) { ax = b.longitude; ay = b.latitude; bx = a.longitude; = a.latitude; } // alter longitude cater 180 degree crossings if (px < 0) { px += 360; }; if (ax < 0) { ax += 360; }; if (bx < 0) { bx += 360; }; if (py == ay || py == by) py += 0.00000001; if ((py > || py < ay) || (px > math.max(ax, bx))) return false; if (px < math.min(ax, bx)) return true; var red = (ax != bx) ? ((by - ay) / (bx - ax)) : float.max_value; var blue = (ax != px) ? ((py - ay) / (px - ax)) : float.max_value; return (blue >= red); } }
in terms of program flow, want background service location updates , perform check against lat/long polygon data see if location inside.
Comments
Post a Comment