Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all nearby customers within a given distance using longitude and latitude

I have a db contain customers longitude and latitude, I have a search form in which the user will enter log/lat and distance dropdown contain 50miles, 100miles,.... and when the user click search I want to write a linq query to get all customers from the db which are in this distance radius. How to make this using C# and linq?

Update:
I found this https://stackoverflow.com/a/1654365/20126 but this gives a square not radius

like image 853
Amr Elgarhy Avatar asked Oct 26 '25 05:10

Amr Elgarhy


1 Answers

A little modification of my answer to a similar question:

// radius is the distance in meters
var center = new GeoCoordinate(latitude, longitude);
var result = customers.Select(x => new GeoCoordinate(x.Latitude, x.Longitude))
                      .Where(x => x.GetDistanceTo(center) < radius);

You need to add reference to System.Device.dll.

like image 109
Fung Avatar answered Oct 28 '25 18:10

Fung