使用纬度和经度的两个位置之间的距离与谷歌所说的相差甚远

发布于 2024-08-08 17:34:41 字数 2857 浏览 5 评论 0原文

我花了几天时间试图解决这个问题,但似乎无法找出问题所在。我有一个 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)

I've spent a few days trying to figure this one out and can't seem to pinpoint the problems. I have a SQL 2005 database storing latitude and longitude as Decimal(18,8), all of which I received by querying Google.

For these two locations: From: 10715 Downsville Pike Ste 100 MD 21740 to: 444 East College Ave Ste 120 State College PA, 16801

Taking into account that distance will be 'as the crow flies', my results are still way off. In this example my result says 21.32 miles, but Google Maps says 144 miles.

I think the topping that makes it even more frustrating is I found this site: http://jan.ucc.nau.edu/~cvm/latlongdist.html and came up with almost the exact same results as me.

Here's my functions and query:

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

Query:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在夏天 2024-08-15 17:34:41

为什么在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

这似乎是合理的。

Why are you resetting @Temp to 1 or -1 in your CalculateDistance function?

Update. Ok, never mind the above. Are you sure your latitude / longitude are correct? I've calculate the following using geocoder.us:

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

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

Using your formulas (rounded to 6 digits after decimal point as that's the precision returned above) you get the following:

  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

which seems reasonable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文