2014-10-01 254 views
0

我想知道如果有一个快速的方法来calc下牵引点纬度和经度之间的距离,我有这样的代码,但它的速度慢MySql的纬度和经度

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

这个返回点1公里的范围内

感谢的提前

+0

这将无法正常工作。经纬度在一个球体上,而不是一架飞机......做一个搜索... – 2014-10-01 06:10:20

+0

我做了一个搜索,我发现它像这个工作,但是很慢。 – skynet 2014-10-01 06:13:12

+0

@shyam我现在没有,谢谢,那么等式呢? – skynet 2014-10-01 06:14:27

回答

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 

您还可以创建值函数从haversine公式标量。

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

+0

好的,但你能提供如何使用,如如何设置经纬度和英里或公里的结果? – skynet 2014-10-01 06:25:02