2

我花了几天时间试图解决这个问题,但似乎无法确定问题所在。我有一个 SQL 2005 数据库,将纬度和经度存储为 Decimal(18,8),所有这些都是通过查询 Google 获得的。

对于这两个地点:从:10715 Downsville Pike Ste 100 MD 21740 到:444 East College Ave Ste 120 State College PA, 16801

考虑到距离将是“乌鸦飞”,我的结果还有很长的路要走。在此示例中,我的结果显示为 21.32 英里,但 Google 地图显示为 144 英里。

我认为最令人沮丧的是我发现了这个网站:http: //jan.ucc.nau.edu/~cvm/latlongdist.html并得出了与我几乎完全相同的结果。

这是我的功能和查询:

函数: 计算距离

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) )

纬度加距离

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

经度加距离

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)
4

1 回答 1

2

为什么在CalculateDistance 函数中将@Temp 重置为1 或-1?

更新。好吧,不管上面的。你确定你的纬度/经度是正确的吗?我使用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

这似乎是合理的。

于 2009-10-22T00:00:56.507 回答