2009-10-21 29 views
2

我花了几天的时间试图找出这一个,似乎无法查明问题。我有一个SQL 2005数据库存储经度和纬度为十进制(18,8),我通过查询Google收到的所有数据。使用经度和纬度的两个位置之间的距离关闭谷歌说的

这两个地点:From: 10715 Downsville Pike Ste 100 MD 21740 to: 444 East College Ave Ste 120 State College PA, 16801

考虑到距离将是“直线距离”,我的成绩依然遥远。在这个例子中,我的结果是21.32英里,但谷歌地图说144英里。

我认为使它更令人沮丧的打顶是我发现这个网站:http://jan.ucc.nau.edu/~cvm/latlongdist.html,并提出几乎与我完全相同的结果。

这里是我的功能和查询:

功能: CalculateDistance

DECLARE @Temp FLOAT 

SET @Temp = SIN(@Latitude1/57.2957795130823) * 
    SIN(@Latitude2/57.2957795130823) + 
    COS(@Latitude1/57.2957795130823) * COS(@Latitude2/57.2957795130823) * 
    COS(@Longitude2/57.2957795130823 - @Longitude1/57.2957795130823) 

IF @Temp > 1 
    SET @Temp = 1 
ELSE IF @Temp < -1 
    SET @Temp = -1 

RETURN (3958.75586574 * ACOS(@Temp)) 

LatitudePlusDistance

RETURN (SELECT @StartLatitude + SQRT(@Distance * @Distance/4766.8999155991)) 

LongitudePlusDistance

RETURN (SELECT @StartLongitude + SQRT(@Distance * @Distance/
    (4784.39411916406 * 
    COS(2 * @StartLatitude/114.591559026165) * 
    COS(2 * @StartLatitude/114.591559026165)))) 

查询:

DECLARE @Longitude DECIMAL(18,8), 
     @Latitude DECIMAL(18,8), 
     @MinLongitude DECIMAL(18,8), 
     @MaxLongitude DECIMAL(18,8), 
     @MinLatitude DECIMAL(18,8), 
     @MaxLatitude DECIMAL(18,8), 
     @WithinMiles DECIMAL(2) 

Set @Latitude = -77.856052 
Set @Longitude = 40.799159 
Set @WithinMiles = 50 

-- Calculate the Max Lat/Long 
SELECT @MaxLongitude = dbo.LongitudePlusDistance(@Longitude, @Latitude, 
      @WithinMiles), 
     @MaxLatitude = dbo.LatitudePlusDistance(@Latitude, @WithinMiles) 

-- Calculate the min lat/long 
SELECT @MinLatitude = 2 * @Latitude - @MaxLatitude, 
     @MinLongitude = 2 * @Longitude - @MaxLongitude 

SELECT Top 20 *, dbo.CalculateDistance(@Longitude, @Latitude, 
    LocationLongitude, LocationLatitude) as 'Distance' 
FROM Location 
WHERE LocationLongitude Between @MinLongitude And @MaxLongitude 
     And LocationLatitude Between @MinLatitude And @MaxLatitude 
     And dbo.CalculateDistance(@Longitude, @Latitude, LocationLongitude, 
      LocationLatitude) <= @WithinMiles 
ORDER BY dbo.CalculateDistance(@Longitude, @Latitude, LocationLongitude, 
    LocationLatitude) 
+3

您链接到的其他网站是从1997年,当地球比今天小得多(由于离心力)。我会和Google一起去看这个。 :) – MusiGenesis 2009-10-22 00:25:12

+0

是的,在谷歌之前,这个世界变得更小了。但是这并不能帮助我达到正确的距离。 – Mikecancook 2009-10-22 01:06:17

+0

我发现了另一个网站来计算结果,它与我一直在得到的一样,21英里和一些变化。 http://www.movable-type.co.uk/scripts/latlong.html 尝试这些位置: 长/纬度 40.799159,-77.856052 40.22018,-78.140205 – Mikecancook 2009-10-22 02:08:05

回答

2

你为什么要重新@temp 1或-1在CalculateDistance功能?

更新。好吧,没关系上述。你确定你的经纬度是正确的吗?我计算使用geocoder.us如下:

10715 Downsville Pike Ste 100, 21740回报(39.607483,-77.753747)

444 E College Ave Ste 120, 16801回报(39.607483,-77.753747)

使用您的公式(四舍五入至6位小数点后的这精确返回上面)你得到以下内容:

sin(39.607483/57.295779) * sin(40.798594/57.295779) 
+ cos(39.607483/57.295779) * cos(40.798594/57.295779) 
* cos(77.753747/57.295779 - 77.856110/57.295779) = 0.99978299 

3958.75586574 * arccos(0.99978299) = 82.4748331 

这似乎是合理的。

+0

好问题!我必须查看我从哪里得到它,这是:http://blogs.lessthandot.com/index。php/DataMgmt/DataDesign/sql-server-zipcode-latitude-longitude-pr 它被解释为适应舍入错误,如果结果超过1或小于-1。 – Mikecancook 2009-10-22 01:53:42

+0

我想知道如何精确地做到这一点所需的精度。我相信我在某处读到只能使用6位小数点,但解释没有意义,现在我无法找到我读的地方。 我一直在使用Google来做我的地理编码。结果似乎有点不一致。我只是不确定有多大的不一致。 – Mikecancook 2009-10-22 05:50:24

+0

好吧,所以经过一些更多的搜索,我发现这个网站:http://www.daftlogic.com/projects-google-maps-distance-calculator.htm - 它使用谷歌API来计算距离,它提出了.. ..drum roll .... 82.530!所以,我似乎需要弄清楚如何获得更准确的地理编码。 – Mikecancook 2009-10-22 05:53:55

相关问题