0
我想知道如果有一個快速的方法來calc下牽引點緯度和經度之間的距離,我有這樣的代碼,但它的速度慢MySql的緯度和經度
SELECT * FROM `hotels` WHERE (SQRT(POW(`lat` - 32.171253 , 2) + POW(`lng` - 35.057362, 2)) * 100) < 1
這個返回點1公里的範圍內
感謝的提前
我想知道如果有一個快速的方法來calc下牽引點緯度和經度之間的距離,我有這樣的代碼,但它的速度慢MySql的緯度和經度
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
您使用它通過連接到包含源和目的地的緯度/多頭如下兩個表:
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
您還可以創建值函數從haversine公式標量。
如果您希望速度更快,請創建一個CLR存儲過程。
好的,但你能提供如何使用,如如何設置經緯度和英里或公里的結果? – skynet 2014-10-01 06:25:02
這將無法正常工作。經緯度在一個球體上,而不是一架飛機......做一個搜索... – 2014-10-01 06:10:20
我做了一個搜索,我發現它像這個工作,但是很慢。 – skynet 2014-10-01 06:13:12
@shyam我現在沒有,謝謝,那麼等式呢? – skynet 2014-10-01 06:14:27