2013-09-01 33 views
1

我的目的是獲取地圖邊界,然後遍歷我的集合並檢查邊界內的哪些點。Bing地圖如何檢查一個點是否在地圖範圍內?

所以第一步是獲得地圖邊界:

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

如果你能幫助我,我會非常感謝! :)

回答

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)); 
相關問題