我正在查询SQL Server中的表格以在Google Maps上显示结果,并且在服务器应用程序中使用Entity Framework和DbGeography数据类型。
简单地说,我从Google Maps实例获取地图边界,然后使用类似以下方法构建一个DbGeography实例:
var wkt = string.Format("POLYGON(({0} {1}, {0} {2}, {3} {2}, {3} {1}, {0} {1}))", EastLng, NorthLat, SouthLat, WestLng);
var bounds = DbGeography.FromText(wkt);
然后使用以下内容(作为更大查询的一部分)查询数据库:
dbset.Where(i => bounds.Intersects(l.GeographicLocation));
这个代码一直运行良好,我已经在生产环境中使用了一段时间。今天我注意到有时候地图上的一些结果显示不正确。
经过一番调查,我意识到这是因为地球曲率的缘故;Google Maps使用地球的矩形投影,但当我创建一个矩形多边形时,它的形状就像这样(从正在运行的查询中复制而来):
效果在查看非常大的区域的地图时会更明显(缩小)。但当我放大地图时,地球曲率的影响减小,查询似乎可以正常工作。
我需要做的就是基本上查询所有位于纬度/经度边界内的点。有办法做到这一点吗?
编辑:
这是我用来创建DbGeography实例的完整代码:
public class LatLngBounds
{
// ...
public DbGeography ToDbGeography()
{
return DbGeographyUtil.CreatePolygon(string.Format("POLYGON(({0} {1}, {0} {2}, {3} {2}, {3} {1}, {0} {1}))", EastLng, NorthLat, SouthLat, WestLng));
}
}
public static class DbGeographyUtil
{
public static DbGeography CreatePolygon(string wktString)
{
if (string.IsNullOrWhiteSpace(wktString))
return null;
var sqlGeography = SqlGeography.STGeomFromText(new SqlChars(wktString), DbGeography.DefaultCoordinateSystemId).MakeValid();
var invertedSqlGeography = sqlGeography.ReorientObject();
if (sqlGeography.STArea() > invertedSqlGeography.STArea())
{
sqlGeography = invertedSqlGeography;
}
return DbSpatialServices.Default.GeographyFromProviderValue(sqlGeography);
}
}
编辑2:
以上示例中谷歌地图输出的坐标是:
- 东北:纬度=53.6 经度=92.5
- 西南:纬度=35.0 经度=21.2
这是我使用SQL Server Profiler捕获到的运行查询的一部分:
declare @p4 sys.geography
set @p4=convert(sys.geography,0xE6100000010405000000463E933FAFD64A40070000006E3F354061E412079A894140070000006E3F354061E412079A894140020000808B245740463E933FAFD64A40020000808B245740463E933FAFD64A40070000006E3F354001000000020000000001000000FFFFFFFF0000000003)
SELECT @p4;
我添加了SELECT
以查看它的外观,结果如上图所示。