Bing Maps如何检查点是否在地图范围内?

时间:2013-09-01 13:31:44

标签: c# entity-framework-5 bing-maps

我的目的是获取地图边界,然后循环我的集合并检查边界内的哪些点。

所以第一步是获得地图边界:

LocationRect bounds = map.Bounds;
Location northwest = bounds.Northwest;
Location southeast = bounds.Southeast;

//Here I converts to DbGeography, because my server side works with it.
DbGeography DbNorthwest = new DbGeography()
{
    Geography = new DbGeographyWellKnownValue()
    {
        CoordinateSystemId = 4326,
        WellKnownText = GeoLocation.ConvertLocationToPoint(new GeocodeService.Location()
        {
            Latitude = northwest.Latitude,
            Longitude = northwest.Longitude
        })
    }
};

DbGeography DbSoutheast = new DbGeography()
{
    Geography = new DbGeographyWellKnownValue()
    {
        CoordinateSystemId = 4326,
        WellKnownText = GeoLocation.ConvertLocationToPoint(new GeocodeService.Location()
        {
            Latitude = southeast.Latitude,
            Longitude = southeast.Longitude
        })
    }
};

现在调用应该返回位于这两个点之间的所有对象的方法:

WcfClient client = new WcfClient();
var results = await client.GetEventsBetweenLocations(DbNorthwest, DbSoutheast);

现在我需要实现这个方法(位于实体框架上): 我不知道怎么做?

public IQueryable<Event> GetEventsBetweenLocations(DbGeography first, DbGeography second)
{
    //How to check if the e.GeoLocation (which is DbGeography type) is between the two locations?
    return this.Context.Events.Where(e => e.GeoLocation ...?);
}

如果你能帮助我,我将非常感谢! :)

1 个答案:

答案 0 :(得分:4)

一个可能的解决方案是找到由NW和SE点定义的边界框内的所有点,使用其众所周知的文本表示构建:

DbGeography boundingBox = DbGeography.FromText(
        string.Format("POLYGON(({0} {1}, {3} {1}, {3} {2}, {0} {2}, {0} {1}))",
                first.Longitude,
                first.Latitude,
                second.Latitude,
                second.Longitude), 4326);

然后你可以找到与这个特定边界框相交的“事件”:

return this.Context.Events.Where(e => e.GeoLocation.Intersects(boundingBox));