我有两个GPS坐标,我想计算它们之间的距离,但是SQL server上的结果与c#中的结果完全不同,我在googled上搜索了这两种方法,发现这两种方法的返回距离(以米为单位),但这种差异让我发疯了。
SQL SERVER查询
select geography::Point(25.3132666, 55.2994054 ,4326)
.STDistance(geography::Point(25.25434, 55.32820,4326)) as Distance;

Web
String format = "POINT(25.25434 55.32820)";
DbGeography myLocation = DbGeography.PointFromText(format, 4326);
var users = context.Users.Select(u => new
{
fullName = u.name,
lat = u.location.Latitude,
lng = u.location.Longitude,
distance = myLocation.Distance(u.location)
}).ToList();响应
,{"fullName":"jons smith","lat":25.3132666,"lng":55.2994054,"distance":4133581.8647264037}提前谢谢。
发布于 2017-05-18 13:06:42
在定义点时,请检查SQL中点的WKT表示形式中的纬度和经度顺序,它们如下所示:GEOGRAPHY::POINT(Latitude, Longitude, srid):
SELECT GEOGRAPHY::Point(25.3132666,55.2994054 ,4326)
.STDistance(GEOGRAPHY::Point(25.25434, 55.32820,4326)) as distance;
//distance: 7142.94965953253但在用DBGeography代码定义时,顺序是不同的:**"POINT(Longitude Latitude)"**
String format = "POINT(55.32820 25.25434)";
DbGeography myLocation = DbGeography.PointFromText(format, 4326);
var users = context.Users.Select(u => new
{
fullName = u.name,
lat = u.location.Latitude,
lng = u.location.Longitude,
distance = myLocation.Distance(u.location)
}).ToList();
//distance: 7142.949659532529此外,您还应该检查插入到Users表中的位置。请确保插入时已正确插入。否则,它们将在其他地方,而不是您的Users的位置
More
SELECT GEOGRAPHY::Point(25, 55 ,4326).Lat //25
DbGeography.PointFromText("POINT(25 55)", 4326).Latitude.Value //55
Microsoft.SqlServer.Types.SqlGeography.STPointFromText(
new System.Data.SqlTypes.SqlChars("POINT(25 55)"), 4326).Lat.Value;
//55Well-Known是什么,它是从哪里来的?,它是OGC在简单特征访问规范中引入的不同几何类型的 Text表示,为了兼容性,建议所有软件供应商都遵循它。这个规范向我们展示了如何定义点、线(线串)、多边形和其他一些几何类型,如文本(WKT)和二进制(WKB)。
SQL Server并没有完全遵循这个规范,我们看到了不遵循标准的结果,甚至在同一家公司的不同组件中也会产生这样的问题。
发布于 2017-05-18 13:06:25
在API版本中切换Lat/Lng。在API中,应该使用Lng Lat。
select geography::Point(25.3132666, 55.2994054 ,4326).STDistance(geography::Point(25.25434, 55.32820,4326)) as Distance返回
7142.94965953253这就是我翻转一个Lat/Lng的UDF的地方。
Select [dbo].[udf-Geo-Meters](55.2994054,25.3132666 ,25.25434,55.32820)返回
4135883.9028193发布于 2017-05-18 13:48:13
这个问题很小,但很棘手。
SQL SERVER查询
geography::Point(25.3132666, 55.2994054 ,4326)Server定义了这样一个点:第一个值是纬度,第二个值是经度。
Web
String format = "POINT(25.25434 55.32820)";
DbGeography myLocation = DbGeography.FromText(format);C#从上述格式定义一个点,使第一个值为经度,第二个值为纬度。
https://stackoverflow.com/questions/44048314
复制相似问题