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?
source share