There is one question about searching in MySQL / long distance in Need help in optimizing lat / Lon geoinformation for mysql
To calculate the distance in C #, most sites use the Haversin formula - here is the C # implementation - http://www.storm-consultancy.com/blog/development/code-snippets/the-haversine-formula-in-c-and-sql / - it also has SQL (MS SQL).
/// <summary> /// Returns the distance in miles or kilometers of any two /// latitude / longitude points. /// </summary> /// <param name="pos1">Location 1</param> /// <param name="pos2">Location 2</param> /// <param name="unit">Miles or Kilometers</param> /// <returns>Distance in the requested unit</returns> public double HaversineDistance(LatLng pos1, LatLng pos2, DistanceUnit unit) { double R = (unit == DistanceUnit.Miles) ? 3960 : 6371; var lat = (pos2.Latitude - pos1.Latitude).ToRadians(); var lng = (pos2.Longitude - pos1.Longitude).ToRadians(); var h1 = Math.Sin(lat / 2) * Math.Sin(lat / 2) + Math.Cos(pos1.Latitude.ToRadians()) * Math.Cos(pos2.Latitude.ToRadians()) * Math.Sin(lng / 2) * Math.Sin(lng / 2); var h2 = 2 * Math.Asin(Math.Min(1, Math.Sqrt(h1))); return R * h2; } public enum DistanceUnit { Miles, Kilometers };
For most queries ... you are probably well sharing work between C # and SQL
- use MySQL to select "closed" lat / lng points, for example. let's say where lat and lng are within 1.0 of your target.
- then use C # to calculate a more accurate distance and choose the "best."
If you used MS SQL 2008, I would recommend using MS SQL geography types because they have built-in optimized indexing and calculation functions. I see that MySQL also has some extensions - http://dev.mysql.com/tech-resources/articles/4.1/gis-with-mysql.html - but I have no experience with them.
source share