[英]System.Data.Spatial DbGeography.Distance is not working consistantly
I am developing an app that needs to calculate the distance between to points. 我正在开发一个需要计算点之间距离的应用程序。 I am using LINQ and the DbGeography class to achieve this.
我正在使用LINQ和DbGeography类来实现此目的。
It is working beautiful in most cases in the app, however I have run into one issue where it does not return the proper result. 在大多数情况下,它在应用程序中都能正常运行,但是我遇到了一个问题,即它无法返回正确的结果。 I please take a look ans see if you spot an issue.
请查看是否发现问题。
I have hard coded values for testing purposes. 我有用于测试目的的硬编码值。
private void GetUsersByDistance()
{
//this code is just for debugging
//end this code is just for debugging
/// this is the center of town in Santa Cruz
var lat = decimal.Parse("36.9741171");
var lng = decimal.Parse("-122.0307963");
// this is about a 1/4 mile away in Santa Cruz
var lat2 = decimal.Parse("36.971524");
var lng2 = decimal.Parse("-122.0166850");
var theUsers = new List<PseudoProfile>();
DbGeography geo = DbGeography.FromText(String.Format("POINT({0} {1})", lng.ToString(CultureInfo.InvariantCulture), lat.ToString(CultureInfo.InvariantCulture)));
var users =
(from u in _WebEntities.Users
select u)
.AsEnumerable()
.Where(u => geo.Distance(DbGeography.FromText("POINT(" + lng2.ToString(CultureInfo.InvariantCulture) + " " + lat2.ToString(CultureInfo.InvariantCulture) + ")")) < 100)
.ToList();
}
This exact code is working fine with other values, so I am stumped. 这个确切的代码可以与其他值一起正常工作,所以我很困惑。 Please help
请帮忙
Thanks 谢谢
.Where(u => geo.Distance(DbGeography.FromText("POINT(" + lng2.ToString(CultureInfo.InvariantCulture) + " " + lat2.ToString(CultureInfo.InvariantCulture) + ")")) < 100)
Your filter will return back only locations with a distance less than 100 meters. 您的过滤器将仅返回距离小于100米的位置。 I have calculated above hard-coded locations and the distance is 1289.0501038954687 meters.
我已经计算出上述硬编码位置,并且距离为1289.0501038954687米。
If your intention is getting users within 100 miles, change to below code to convert meters to miles 如果您打算吸引用户100英里以内,请更改为以下代码,以将米转换为英里
.Where(u => geo.Distance(DbGeography.FromText("POINT(" + lng2.ToString(CultureInfo.InvariantCulture) + " " + lat2.ToString(CultureInfo.InvariantCulture) + ")")) / 1609.34 < 100)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.