Vipul Odhavani
Vipul Odhavani

Reputation: 127

How to search Latitude and Longitude based record from SQL Server use Asp.net MVC and C#

I want to search all records from my SQL Server database, based on some conditions
like "Near by 5 Km area", "people from my present location" (Male, Female, Both) etc.

The table structure looks like this:

Table structure

So, how can I do this?

Thanks

Upvotes: 1

Views: 288

Answers (2)

Arkadiusz Raszeja
Arkadiusz Raszeja

Reputation: 882

I'm afraid you'll be force to use something like google maps API for c# to count the distance.

Otherwise, maybie you have something like additional "distance from" table, but I don't think so...
In general even if you have some table with columns like Addres, Gender it's still not that easy to perform:
Distance between addresses Solution from that answer:

public decimal calcDistance(decimal latA, decimal longA, decimal latB, decimal longB)
{

    double theDistance = (Math.Sin(DegreesToRadians(latA)) *
            Math.Sin(DegreesToRadians(latB)) +
            Math.Cos(DegreesToRadians(latA)) *
            Math.Cos(DegreesToRadians(latB)) *
            Math.Cos(DegreesToRadians(longA - longB)));

    return Convert.ToDecimal((RadiansToDegrees(Math.Acos(theDistance)))) * 69.09M * 1.6093M;
}

Upvotes: 0

Mike Gledhill
Mike Gledhill

Reputation: 29161

Have a read of my StackOverflow answer here:

Get the nearest longitude and latitude from MSSQL database table?

Basically, you can use the SQL Server geography type, then use STDistance.

This article includes an example, showing exactly how to use it, and also how to use Pythagorus, to calculate the distances even quicker.

Upvotes: 2

Related Questions