複数の地理的位置 (経度、緯度の値) からポリゴン ジオフェンスを作成する方法。また、ユーザーを追跡する方法は、このジオフェンス領域に入るか、Android でこの領域から出るかです。
11729 次
2 に答える
19
ジオフェンスは、ポリゴンを形成する緯度/経度のポイントの配列です。緯度/経度ポイントのリストを取得したら、ポリゴン内ポイント チェックを使用して、位置がポリゴン内にあるかどうかを確認できます。
これは、私自身のプロジェクトで、非常に大きな凹型ポリゴン (20K+ 頂点) のポイント イン ポリゴン チェックを実行するために使用したコードです。
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);
}
}
プログラム フローに関しては、バックグラウンド サービスで位置の更新を行い、緯度/経度のポリゴン データに対してこのチェックを実行して、位置が内部にあるかどうかを確認する必要があります。
于 2013-08-28T11:40:04.393 に答える