使用纬度和经度返回 SQL Server 2008 中两个位置之间的距离
我们有一张表,其中包含地点及其纬度和经度。
我们正在尝试在 SQL Server 2008 中创建一个函数,以使用特定的纬度和经度作为中心点列出接下来 25 公里内的地点。
我想知道这是否是启动和测试我们的功能并获取中心点(当前位置)和目标位置(@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
它是正确的还是我们遗漏了一些东西?
We have a table with places and their latitudes and longitudes.
We are trying to create a function in SQL Server 2008 to list places within next 25 kilometers using a specific latitude and longitude as centre point.
I was wandering if this is a good way to start and test our function and getting current distance between a centre point (current location) and a 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 it correct or we are missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您正在使用 大圆距离 公式,该公式可能足够准确你,尽管你必须对此做出判断。
如果您想检查公式的结果,可以使用地理位置 数据类型:
并且由于您正在执行 邻近搜索,您可能需要进一步研究
geography
数据类型。It looks like you are using the great-circle distance formula, which is probably accurate enough for you, although you'll have to be the judge of that.
If you want to check the results of your formula, you can use the geography data type:
and since you are doing a proximity search, you may want to investigate the
geography
data type further.这有效吗?
谢谢!
Would this be valid?
Thanks!