0

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.

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

  • You're not using `u` for anything. All rows are compared using the same two coordinates... So they'll either all return or none will return back (depending on the distance). Your essentially comparing `geo` with the same (hypothetical) `geo2` for every row. If that's not the actual case, you need to explain what you're seeing. – pinkfloydx33 Jul 13 '18 at 22:35
  • yeah, I did not want to boar people with the actual method I wrote , so I just showed the code that was using the DbGeography class. In the real code coordinates are values coming from the db and from the phone, I figured out the solution quickly after I posted this, but thank you for looking and replying. – Ian Thomas Parks Jul 14 '18 at 23:31
  • Well, you should show it (add it to your question and fix the example). Like I said, with your example as written I'd expect either all rows or no rows to return. – pinkfloydx33 Jul 14 '18 at 23:43
  • there is nothing wrong with my example. I use two constants instead of showing you data from a table, and data from a phone. And you are incorrect about all or no rows returning. – Ian Thomas Parks Jul 16 '18 at 03:13

1 Answers1

0
.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. I have calculated above hard-coded locations and the distance is 1289.0501038954687 meters.

If your intention is getting users within 100 miles, change to below code to convert meters to miles

.Where(u => geo.Distance(DbGeography.FromText("POINT(" + lng2.ToString(CultureInfo.InvariantCulture) + " " + lat2.ToString(CultureInfo.InvariantCulture) + ")")) / 1609.34 < 100)
Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67