【问题标题】:How can I check whether a location is within a MapPolygon in the Silverlight Bing Maps control?如何检查某个位置是否在 Silverlight Bing 地图控件的 MapPolygon 内?
【发布时间】:2010-07-20 20:11:11
【问题描述】:

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

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

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

是否也可以判断两个 MapPolygon 是否相交?

【问题讨论】:

标签: silverlight location bing-maps


【解决方案1】:

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 
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 是的,只需使用链接文章中提供的功能函数pointInPolygon(points,lat,lon)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    相关资源
    最近更新 更多