实现地理围栏 - C#

3

我需要在C#中实现地理围栏。地理围栏区域可以是圆形、矩形、多边形等。有没有人在C#中实现过地理围栏?

我找到了Geo Fencing - point inside/outside polygon。但是,它仅支持多边形。


多边形显然包含矩形作为一种特殊情况(前提是您设法定义出球面上的矩形)。而圆可以用勾股定理进行检查。 - CodesInChaos
3个回答

6
我已经测试了各种不同的实现方式,而这个例子对我来说运行得非常正常:

示例

public static bool PolyContainsPoint(List<Point> points, Point p) {
    bool inside = false;

    // An imaginary closing segment is implied,
    // so begin testing with that.
    Point v1 = points[points.Count - 1];

    foreach (Point v0 in points)
    {
        double d1 = (p.Y - v0.Y) * (v1.X - v0.X);
        double d2 = (p.X - v0.X) * (v1.Y - v0.Y);

        if (p.Y < v1.Y)
        {
            // V1 below ray
            if (v0.Y <= p.Y)
            {
                // V0 on or above ray
                // Perform intersection test
                if (d1 > d2)
                {
                    inside = !inside; // Toggle state
                }
            }
        }
        else if (p.Y < v0.Y)
        {
            // V1 is on or above ray, V0 is below ray
            // Perform intersection test
            if (d1 < d2)
            {
                inside = !inside; // Toggle state
            }
        }

        v1 = v0; //Store previous endpoint as next startpoint
    }

    return inside; 
}

1
很棒的回答,Fnascimento!! - Diogo Cid

1

你对于圆形的实现是什么?我在你的名字中没有找到任何答案。 - ShivaPrasad

0

在这里添加C#实现
对我很有效!

//Location will hold the latitude and longitude.
public class Location
{
    public double lat { get; set; }
    public double lng { get; set; }
    public Location(double lat, double lng)
    {
        this.lat = lat;
        this.lng = lng;
    }
}
//Implementation for the Polygon.

bool IsPointInPolygon(List<Location> poly, Location point)
{
        int i, j;
        bool c = false;
        for (i = 0, j = poly.Count - 1; i < poly.Count; j = i++)
        {
            if ((((poly[i].lat <= point.lat) && (point.lat < poly[j].lat))
                    || ((poly[j].lat <= point.lat) && (point.lat < poly[i].lat)))
                    && (point.lng < (poly[j].lng - poly[i].lng) * (point.lat - poly[i].lat)
                        / (poly[j].lat - poly[i].lat) + poly[i].lng))
            {

                c = !c;
            }
        }
        return c;
}

//Geofencing for the Circle.
//GetDistance will return total Kilometers
//p1 is the Center lat,long and p2 is the current location lat,long
//radius in meters 

public bool IsPointInCircle(Location p1,Location p2,double radius)
{
        return GetDistance(p1,p2)>radius*0.001?false:true;
}

public double GetDistance(Location pos1, Location pos2)
{
        double e = pos1.lat * (Math.PI / 180);
        double f = pos1.lng * (Math.PI / 180);
        double g = pos2.lat * (Math.PI / 180);
        double h = pos2.lng * (Math.PI / 180);
        double i =
            (Math.Cos(e) * Math.Cos(g) * Math.Cos(f) * Math.Cos(h)
            + Math.Cos(e) * Math.Sin(f) * Math.Cos(g) * Math.Sin(h)
            + Math.Sin(e) * Math.Sin(g));
        double j = Math.Acos(i);
        return (6371 * j);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接