Search code examples
sql-servergeolocationneighbours

Finding markers within a SQL Server database from a google maps startingpoint with a radius


I'm new to geodata and I am playing around with google maps, asp.net and SQL Server, but I'm a bit stuck.

I got a SQL Server table with markers with there longitude and latitude.

And I want to get the 10 nearest markers from the database relative to a startingpoint.

What is the best way to do this.

The server is a SQL 2005 and the values are stored in two columns datatype nvarchar(300)


Solution

  • You don't need to use C# or ASP.NET for this reason, just use SQL request. Here is example from AdventureDataworks sample database for finding neighboring locations:

    USE AdventureWorks2008R2
    GO
    DECLARE @g geography = 'POINT(-121.626 47.8315)';
    SELECT TOP(7) SpatialLocation.ToString(), City FROM Person.Address
    WHERE SpatialLocation.STDistance(@g) IS NOT NULL
    ORDER BY SpatialLocation.STDistance(@g);