gpt4 book ai didi

MySql 纬度和经度

转载 作者:行者123 更新时间:2023-11-29 12:40:56 25 4
gpt4 key购买 nike

我想知道是否有一种快速的方法来计算两个点 lat 和 lng 之间的距离我有这个代码,但它很慢

SELECT * FROM `hotels` WHERE (SQRT(POW(`lat` - 32.171253 , 2) + POW(`lng` - 35.057362, 2)) * 100) < 1

此返回点在 1 公里左右

提前致谢

最佳答案

CREATE FUNCTION dbo.DistanceKMFromLatLong
(
@srcLatitude decimal(9,6),
@srcLongitude decimal(9,6),
@destLatitude decimal(9,6),
@destLongitude decimal(9,6)
)
RETURNS TABLE
AS
RETURN
(
SELECT DistanceKM = ACOS(SIN(PI()*@srcLatitude/180.0)*SIN(PI()*@destLatitude/180.0)+COS(PI()*@srcLatitude/180.0)*COS(PI()*@destLatitude/180.0)*COS(PI()*@destLongitude/180.0-PI()*@srcLongitude/180.0))*6371
)
GO

引用号:Haversine formula

您可以通过连接两个包含源和目标纬度/经度的表来使用它,如下所示:

select 
p.Latitude,
p.Longitude,
s.Latitude,
s.Longitude,
DistanceFromStoreKMNew = d.DistanceKM
from
Staging.PostcodesToLatLong p
join dbo.Store s on s.StoreId = p.StoreId
cross apply dbo.DistanceKMFromLatLong(p.Latitude, p.Longitude, s.Latitude, s.Longitude) d

您还可以根据半正弦公式创建标量值函数。

如果您希望速度更快,请创建一个 CLR 存储过程。

关于MySql 纬度和经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26135096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com