0

I have many locations in Google Maps. And in database(SQL Server) I stored their coordinates. Model is like this:

public class Marker
{
   public int Id { get; set; }
   public string Lat { get; set; }
   public string Long { get; set; }
}

1 coordinate(latitude and longitude) and 1 radius (i.e 100 meter) will given to me, I should to find locations in this area inside radius from locations list.

How can I calculate distance by radius on coordinates?

Jeyhun Rahimov
  • 3,769
  • 6
  • 47
  • 90
  • I'm not sure what you're after. Are you wanting to see if your marker is within a given distance of another location? – Adrian Wragg Sep 04 '13 at 12:28
  • Yes, I need to calculate the distances between given coordinate and others, then if distance is less than given radius, to take that markers, to filter marker list. To solve this I need to calculate the distance between locations with meter in c#. – Jeyhun Rahimov Sep 04 '13 at 12:33

2 Answers2

1

I think you need use Haversine formula foreach point from given point and compare result with radius.

progpow
  • 1,560
  • 13
  • 26
1

The following SQL query uses Spherical Law of Cosines to calculate the distance between a coordinate and coordinates in a table.

d = acos( sin(φ1).sin(φ2) + cos(φ1).cos(φ2).cos(Δλ) ).R

The query uses SQL Math functions

"SELECT Id,Lat,Long,(6367 * acos( cos( radians(center_lat) )
  * cos( radians( Lat ) ) * cos( radians( Long ) - radians(center_lng) )
  + sin( radians(center_lat) ) * sin( radians( Lat ) ) ) ) AS distance FROM table 
  HAVING distance < radius ORDER BY distance ASC LIMIT 0 , 20"

Using prepared statement for MS SQL & C#.

Try

private static void SqlCommandPrepareEx(string connectionString)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        SqlCommand command = new SqlCommand(null, connection);

        // Create and prepare an SQL statement.
        command.CommandText =
        "SELECT  Id, Lat, Long, (6367 * acos( cos( radians(@center_lat) ) * cos( radians( Lat ) ) * cos( radians( Long ) - radians(@center_lng) ) + sin( radians(@center_lat) ) * sin( radians( Lat ) ) ) ) AS distance FROM table HAVING distance < @radius ORDER BY distance ASC LIMIT 0 , 20");
        SqlParameter center_lat = new SqlParameter("@center_lat", SqlDbType.Decimal);
        center_lat.Precision = 10;
        center_lat.Scale = 6;
        center_lat.Value = YOURVALUEHERE;//latitude of centre
        command.Parameters.Add(center_lat);
        SqlParameter center_lng = new SqlParameter("@center_lng", SqlDbType.Decimal);
        center_lng.Precision = 10;
        center_lng.Scale = 6;
        center_lng.Value = YOURVALUEHERE;//longitude of centre
        command.Parameters.Add(center_lng);
        SqlParameter radius = new SqlParameter("@radius", SqlDbType.Int,3);
        radius.Value = YOURVALUEHERE;
        command.Parameters.Add(radius);//Radius in km
        // Call Prepare after setting the Commandtext and Parameters.
        command.Prepare();
        command.ExecuteNonQuery();


    }
}

As I don't have MS SQL & C# I can't test

PS use 3956 for miles 6367 for kms

david strachan
  • 7,174
  • 2
  • 23
  • 33