简体   繁体   English

System.Data.Spatial DbGeography.Distance不能始终如一地工作

[英]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.

相关问题 System.Data.Spatial DbGeography.Distance单位? - System.Data.Spatial DbGeography.Distance units? System.Data.Spatial DBGeography.Distance()将与SQL Server 2008一起使用吗? - Will System.Data.Spatial DBGeography.Distance() will work with SQL Server 2008? System.Data.Entity.Spatial.DbGeography带有标高的距离 - System.Data.Entity.Spatial.DbGeography Distance with elevation System.Data.Spatial或System.Data.Entity.Spatial - System.Data.Spatial or System.Data.Entity.Spatial DbGeography.Distance 返回不正确的结果 - DbGeography.Distance returned incorrect result 无法将System.Data.Spatial添加到我的Domain类 - Cannot add System.Data.Spatial to my Domain classes Peta Poco和System.Data.Entity.Spatial.DbGeography - Peta Poco and System.Data.Entity.Spatial.DbGeography System.Data.Entity.Spatial.DbGeography-没有无参数构造函数 - System.Data.Entity.Spatial.DbGeography - No Parameterless Constructor 将 System.Data.Entity.Spatial.DbGeography 绑定到 MVC Model - Bind System.Data.Entity.Spatial.DbGeography to MVC Model 如何在 Code First Entity Framework 中返回 DbGeography.Distance 计算值而不丢失强类型? - How to return DbGeography.Distance calculated value In Code First Entity Framework without losing strong typing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM