I'm new to coding in general, and am working on a social networking project in ASP.NET. I have to come up with a way to display nearby users based on the location they have set in their profile. It involves retrieving that piece of data (their set location) from SQLServer, so I can display relevant users.
How would I go about doing this? I am using C# as the controller, and need to reference it in an html document. I appreciate any help I can get.
Thanks!!!
Bit if a broad topic, so if I may suggest that you start with the database and worry about the UI later.
Let's assume you have a table of users, and their current locations are logged into the database.
Now, you want to find all within a 5 mile radius. Consider the following. Note @Users is just a table variable and would be replaced with your actual table.
Declare @Users table (ID int,UserName varchar(25),Lat float,Lng float)
Insert Into @Users values
(1,'James',41.721913,-71.265238),
(2,'Joseph',41.709084,-71.386673),
(3,'Mary',41.709084,-71.386673),
(4,'Sam',41.68745,-71.270122),
(5,'Sally',41.756903,-71.222215),
(6,'John',41.744068,-71.315024) -- Let's assume this is my current position
Declare @MyLat float = 41.744068
Declare @MyLng float = -71.315024
Declare @Miles float = 5
Select MilesAway = [dbo].[udf-Geo-Calc-Miles] (@MyLat,@MyLng,A.Lat,A.Lng)
,A.*
From @Users A
Where [dbo].[udf-Geo-Calc-Miles] (@MyLat,@MyLng,A.Lat,A.Lng) <= @Miles
Order by 1
Returns
Now, I use a UDF to calculate the miles (can be converted to meters)
CREATE Function [dbo].[udf-geo-Calc-Miles] (@Lat1 float,@Lng1 float,@Lat2 Float,@Lng2 float)
Returns Float as
Begin
Declare @Miles Float = (Sin(Radians(@Lat1)) * Sin(Radians(@Lat2))) + (Cos(Radians(@Lat1)) * Cos(Radians(@Lat2)) * Cos(Radians(@Lng2) - Radians(@Lng1)))
Return Case When @Miles is null then 0 else abs((3958.75 * Atan(Sqrt(1 - power(@Miles, 2)) / @Miles))) end
End
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.