Skip to main content
 首页 » 编程设计

c#之Bing Maps 如何检查一个点是否在 map 范围内

2024年02月13日33wayfarer

我的目的是获取 map 边界,然后遍历我的集合并检查哪些点在边界内。

所以第一步是获取 map 边界:

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); 

现在我需要实现这个方法(位于 Entity Framework 上): 不知道怎么办?

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 ...?); 
} 

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

请您参考如下方法:

一个可能的解决方案是找到由 NW 和 SE 点定义的边界框内的所有点,使用其 Well-known-text 表示构建:

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));