Md Uddin
Md Uddin

Reputation: 13

Find address by latitude and longitude from a point (lat, lng) given

I have a table structure (and data) with the following information

[Note: data provided below are just sample and randomly added, please excuse if the longitude and latitude values are not valid in this sample data.]

  Id |  FullAddress    | Longitude | Latitude | 
  ----------------------------------------
  1  | 123 Test Ave, Toronto, ON, M1L3T8, Canada  | 43.12345  | -72.2355 |
  ------------------------------------------
  2  | 7 North Ave, Brampton, ON, L7A1T9, Canada | 40.12345  | -71.2745 |
  ------------------------------------------
  5  | 10 South Dr., Toronto, ON, M4H1J4, Canada  | 42.45375  | -73.2645 |
  ------------------------------------------
  8  | 123A Ave Rd, Ajax, ON, M3K3Y9, Canada   | 41.09775  | -74.2745 |
  ------------------------------------------
.
.
.

A point with latitude and longitude is given. For example: (43.54567, -73.5433).

From this point ((43.54567, -73.5433)) within 1 KM range (this can be any number), I would like to find all the address from the above table using the table longitude and latitude data (all the address within 1 KM range of the point given).

enter image description here

If we consider the above example (from the picture), the query should return Address with ID 1 (1 KM) and 3 (0.8 KM).

I am using MS SQL with ASP.NET MVC. I can use LINQ command for querying the database if that makes it easier.

Thank you in advance.

Upvotes: 1

Views: 2395

Answers (2)

Win
Win

Reputation: 62260

You can use the following query to find addresses in given range, and order by nearest. You can read more at MapPoint in .Net.

DECLARE @CntXAxis FLOAT 
DECLARE @CntYAxis FLOAT 
DECLARE @CntZAxis FLOAT 


SET @CntXAxis = COS(RADIANS(-118.4104684)) * COS(RADIANS(34.1030032)) 
SET @CntYAxis = COS(RADIANS(-118.4104684)) * SIN(RADIANS(34.1030032)) 
SET @CntZAxis = SIN(RADIANS(-118.4104684)) 

SELECT 
        50 *,
    ProxDistance = 3961 * ACOS( dbo.XAxis(LAT, LONG)*@CntXAxis + dbo.YAxis(LAT, LONG)*@CntYAxis + dbo.ZAxis(LAT)*@CntZAxis) 
FROM 
    tbl_ProviderLocation 
WHERE 
    (3961 * ACOS( dbo.XAxis(LAT, LONG)*@CntXAxis + dbo.YAxis(LAT, LONG)*@CntYAxis + dbo.ZAxis(LAT)*@CntZAxis) <= 10) 
ORDER BY 
    ProxDistance ASC

User Defined Functions

CREATE FUNCTION [dbo].[XAxis] (@lat float, @lon float)  
RETURNS float
AS  
BEGIN 
   RETURN COS(4 * (4 * atn2(1, 5) - atn2(1, 239)) / 180 * @lat) * COS(4 * (4 * atn2(1, 5) - atn2(1, 239)) / 180 * @lon) 
END

CREATE FUNCTION [dbo].[YAxis] (@lat float, @lon float)  
RETURNS float AS  
BEGIN 
RETURN COS(4 * (4 * atn2(1,5) - atn2(1,239)) / 180 * @lat) * SIN(4 * (4 * atn2(1,5) - atn2(1,239)) / 180 * @lon)
END

CREATE FUNCTION [dbo].[ZAxis] (@lat float)  
RETURNS float AS  
BEGIN 
RETURN SIN(4 * (4 * atn2(1,5) - atn2(1,239)) / 180 * @lat)
END

Upvotes: 0

John Cappelletti
John Cappelletti

Reputation: 81930

If you are not using the GEOGRAPHY type in SQL, you can do something like this

Example

Declare @BaseLat float = 43.54567
Declare @BaseLng float = -73.5433

Select * 
 From  YourTable 
 Where [dbo].[udf-Geo-Meters](@BaseLat,@BaseLng,Latitude ,Longitude ) <=1000

The UDF if needed

CREATE Function [dbo].[udf-Geo-Meters](@Lat1 FLOAT, @Lng1 FLOAT, @Lat2 FLOAT, @Lng2 FLOAT)
Returns Float as
Begin
    Return ACOS(SIN(PI()*@Lat1/180.0)*SIN(PI()*@Lat2/180.0)+COS(PI()*@Lat1/180.0)*COS(PI()*@Lat2/180.0)*COS(PI()*@Lng2/180.0-PI()*@Lng1/180.0)) * 6371008.8
    -- 6.371 mean radius of earth in meters
End

Upvotes: 1

Related Questions