Return distance between two points in SQL Server 2008 using latitude and longitude

We have a table with places, their latitudes and longitudes.

We are trying to create a function in SQL Server 2008 to list locations over the next 25 kilometers using a specific latitude and longitude as the center point.

I wandered if this is a good way to start and test our function and get the current distance between the center point (current location) and the target location (@ latitude / longitude @):

ALTER FUNCTION [dbo].[GetDistanceFromLocation] ( @myCurrentLatitude float, @myCurrentLongitude float, @latitude float, @longitude float ) RETURNS int AS BEGIN DECLARE @radiusOfTheEarth int SET @radiusOfTheEarth = 6371--km DECLARE @distance int SELECT @distance = ( @radiusOfTheEarth * acos( cos( radians(@myCurrentLatitude) ) * cos( radians( @latitude ) ) * cos( radians( @longitude ) - radians(@myCurrentLongitude) ) + sin( radians(@myCurrentLatitude) ) * sin( radians( @latitude ) ) ) ) RETURN @distance END 

Is this right or is something missing?

+6
source share
2 answers

It sounds like you are using the big circle formula, which is probably accurate enough for you, although you will need to judge that.

If you want to check the results of your formula, you can use the geography data type:

 declare @geo1 geography = geography::Point(@lat1, @long1, 4326), @geo2 geography = geography::Point(@lat2, @long2, 4326) select @geo1.STDistance(@geo2) 

and as you search, you may need to continue to explore the geography data type.

+11
source

Will it really be?

 CREATE FUNCTION [dbo].[GetDistanceFromLocation] ( @CurrentLatitude float, @CurrentLongitude float, @latitude float, @longitude float ) RETURNS int AS BEGIN DECLARE @geo1 geography = geography::Point(@lat1, @long1, 4268), @geo2 geography = geography::Point(@lat2, @long2, 4268) DECLARE @distance int SELECT @distance = @geo1.STDistance(@geo2) RETURN @distance END 

Thanks!

0
source

Source: https://habr.com/ru/post/897703/


All Articles