如何检查某个位置是否位于 Silverlight Bing 地图控件中的 MapPolygon 内?

发布于 2024-09-08 18:22:50 字数 346 浏览 0 评论 0原文

我有一个 MapPolygon,它覆盖了 Silverlight Bing Maps 控件上的某个区域, 我想知道某个特定位置是否位于此 MapPolygon 内。

我尝试了以下代码,但它没有返回我想要的结果,因为它只检查测试的位置是否是 MapPolygon 的顶点之一,并且不检查此位置是否包含在该 MapPolygon 中。

polygon.Locations.Contains(new Location(this.Site.Latitude, this.Site.Longitude, this.Site.Altitude));

是否也可以确定两个 MapPolygon 是否彼此相交?

I have a MapPolygon which covers a certain area on the Silverlight Bing Maps control,
and I would like to know if a particular Location is located within this MapPolygon.

I have tried the following code which doesen't return the result I want because it only checks if the tested location is one of the vertices of the MapPolygon, and doesn't check if this Location is contained within this MapPolygon.

polygon.Locations.Contains(new Location(this.Site.Latitude, this.Site.Longitude, this.Site.Altitude));

Is it also possible to determine if two MapPolygons intersect one another?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断舍离 2024-09-15 18:22:50

Polygon.Locations 是定义多边形的点的列表。

您必须制定一种方法来查找您的点是否在多边形内部。

使用类似这样的东西(如果编译则未测试):

static bool PointInPolygon(LocationCollection polyPoints, Location point)
{

    if (polyPoints.Length < 3)
    {
        return false;
    }

    bool inside = false;
    Location p1, p2;

    //iterate each side of the polygon
    Location oldPoint = polyPoints[polyPoints.Count - 1];

    foreach(Location newPoint in polyPoints)
    {
        //order points so p1.lat <= p2.lat;
        if (newPoint.Latitude > oldPoint.Latitude)
        {
            p1 = oldPoint;
            p2 = newPoint;
        }
        else
        {
            p1 = newPoint;
            p2 = oldPoint;
        }

        //test if the line is crossed and if so invert the inside flag.
        if ((newPoint.Latitude < point.Latitude) == (point.Latitude <= oldPoint.Latitude)
            && (point.Longitude - p1.Longitude) * (p2.Latitude - p1.Latitude)
             < (p2.Longitude - p1.Longitude) * (point.Latitude - p1.Latitude))
        {
            inside = !inside;
        }

        oldPoint = newPoint;
    }

    return inside;
}

并像这样调用它:

if (PointInPolygon(polygon.Locations, new Location(this.Site.Latitude, this.Site.Longitude, this.Site.Altitude)))
{
    //do something 
}

The polygon.Locations is a list of points defining the polygon.

You have to make a method to find if your point is inside the polygon.

Use something like this (not tested if compiles):

static bool PointInPolygon(LocationCollection polyPoints, Location point)
{

    if (polyPoints.Length < 3)
    {
        return false;
    }

    bool inside = false;
    Location p1, p2;

    //iterate each side of the polygon
    Location oldPoint = polyPoints[polyPoints.Count - 1];

    foreach(Location newPoint in polyPoints)
    {
        //order points so p1.lat <= p2.lat;
        if (newPoint.Latitude > oldPoint.Latitude)
        {
            p1 = oldPoint;
            p2 = newPoint;
        }
        else
        {
            p1 = newPoint;
            p2 = oldPoint;
        }

        //test if the line is crossed and if so invert the inside flag.
        if ((newPoint.Latitude < point.Latitude) == (point.Latitude <= oldPoint.Latitude)
            && (point.Longitude - p1.Longitude) * (p2.Latitude - p1.Latitude)
             < (p2.Longitude - p1.Longitude) * (point.Latitude - p1.Latitude))
        {
            inside = !inside;
        }

        oldPoint = newPoint;
    }

    return inside;
}

And call it like this:

if (PointInPolygon(polygon.Locations, new Location(this.Site.Latitude, this.Site.Longitude, this.Site.Altitude)))
{
    //do something 
}
泼猴你往哪里跑 2024-09-15 18:22:50

当然,这两件事都是相当微不足道的,请看下面的文章。 http://msdn.microsoft.com/en-us/library/cc451895。 aspx 它为边界框、半径和多边形搜索提供了很好的方法。特别注意 pointInPolygon 方法。

Sure both of these things are fairly trivial, take a look at the following article. http://msdn.microsoft.com/en-us/library/cc451895.aspx It gives good methods for Bounding Box, Radius, and Polygon Search. Particularity take note of the pointInPolygon method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文