問題描述
如何從多個地理位置(經(jīng)緯度值)創(chuàng)建多邊形地理圍欄.此外,如何跟蹤用戶是進入這個地理圍欄區(qū)域還是在 android 上退出這個區(qū)域.
How to create Polygon Geofence from multiple geo locations(long,lat values) . Also how to track user is entering into this geofence region or exiting from this region on android.
推薦答案
地理圍欄只是一個組成多邊形的緯度/經(jīng)度點的數(shù)組.獲得緯度/經(jīng)度點列表后,您可以使用點內多邊形檢查來查看某個位置是否在多邊形內.
A geofence is simply an array of lat/long points that form a polygon. Once you have a list of lat/long points, you can use a point-inside-polygon check to see if a location is within the polygon.
這是我在自己的項目中用于對非常大的凹多邊形(20K+ 頂點)執(zhí)行多邊形點檢查的代碼:
This is code I have used in my own projects to perform point-in-polygon checks for very 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;
// for each edge
for (var i=0; i < count; i++)
{
var a = thePath [i];
var j = i + 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 by = b.Latitude;
if (ay > by)
{
ax = b.Longitude;
ay = b.Latitude;
bx = a.Longitude;
by = a.Latitude;
}
// alter longitude to cater for 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 > by || 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);
}
}
就程序流程而言,您需要后臺服務進行位置更新,然后針對您的緯度/經(jīng)度多邊形數(shù)據(jù)執(zhí)行此檢查,以查看該位置是否在內部.
In terms of program flow, you will want a background service to do location updates and then perform this check against your lat/long polygon data to see if the location is inside.
這篇關于Android 地理圍欄(多邊形)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!