计算两点之间的距离(纬度,经度)

我正在尝试计算地图上两个位置之间的距离。我已经在数据中存储了:经度,纬度,X POS和Y POS。


我以前一直在使用以下代码段。


DECLARE @orig_lat DECIMAL

DECLARE @orig_lng DECIMAL

SET @orig_lat=53.381538 set @orig_lng=-1.463526

SELECT *,

    3956 * 2 * ASIN(

          SQRT( POWER(SIN((@orig_lat - abs(dest.Latitude)) * pi()/180 / 2), 2) 

              + COS(@orig_lng * pi()/180 ) * COS(abs(dest.Latitude) * pi()/180)  

              * POWER(SIN((@orig_lng - dest.Longitude) * pi()/180 / 2), 2) )) 

          AS distance

--INTO #includeDistances

FROM #orig dest

但是,我不相信由此产生的数据,它似乎给出了稍微不准确的结果。


一些样本数据,以备不时之需


Latitude        Longitude     Distance 

53.429108       -2.500953     85.2981833133896

任何人都可以帮助我解决我的代码,如果您有一种新的方法来实现这一点,那我就介意您是否要解决已有的问题。


请说明您的结果采用的计量单位。


哔哔one
浏览 734回答 3
3回答

MYYA

由于您使用的是SQL Server 2008,因此具有geography可用的数据类型,该数据类型专门用于此类数据:DECLARE @source geography = 'POINT(0 51.5)'DECLARE @target geography = 'POINT(-3 56)'SELECT @source.STDistance(@target)给----------------------538404.100197555(1 row(s) affected)告诉我们,从(伦敦附近)到(爱丁堡附近)大约538公里。当然,首先需要学习很多东西,但是一旦您知道它,就比实现自己的Haversine计算容易得多。再加上您可以获得很多功能。如果您想保留现有的数据结构,则仍然可以使用STDistance,方法是geography使用以下Point方法构造合适的实例:DECLARE @orig_lat DECIMAL(12, 9)DECLARE @orig_lng DECIMAL(12, 9)SET @orig_lat=53.381538 set @orig_lng=-1.463526DECLARE @orig geography = geography::Point(@orig_lat, @orig_lng, 4326);SELECT *,    @orig.STDistance(geography::Point(dest.Latitude, dest.Longitude, 4326))        AS distance--INTO #includeDistancesFROM #orig dest

慕码人8056858

下面的函数给出两个地理坐标之间的距离(以英里为单位)create function [dbo].[fnCalcDistanceMiles] (@Lat1 decimal(8,4), @Long1 decimal(8,4), @Lat2 decimal(8,4), @Long2 decimal(8,4))returns decimal (8,4) asbegindeclare @d decimal(28,10)-- Convert to radiansset @Lat1 = @Lat1 / 57.2958set @Long1 = @Long1 / 57.2958set @Lat2 = @Lat2 / 57.2958set @Long2 = @Long2 / 57.2958-- Calc distanceset @d = (Sin(@Lat1) * Sin(@Lat2)) + (Cos(@Lat1) * Cos(@Lat2) * Cos(@Long2 - @Long1))-- Convert to milesif @d <> 0beginset @d = 3958.75 * Atan(Sqrt(1 - power(@d, 2)) / @d);endreturn @dend&nbsp;下面的函数给出了两个地理坐标之间的距离(以千米为单位)CREATE FUNCTION dbo.fnCalcDistanceKM(@lat1 FLOAT, @lat2 FLOAT, @lon1 FLOAT, @lon2 FLOAT)RETURNS FLOAT&nbsp;ASBEGIN&nbsp; &nbsp; RETURN ACOS(SIN(PI()*@lat1/180.0)*SIN(PI()*@lat2/180.0)+COS(PI()*@lat1/180.0)*COS(PI()*@lat2/180.0)*COS(PI()*@lon2/180.0-PI()*@lon1/180.0))*6371END下面的函数 使用sql server 2008中引入的Geography数据类型,以公里为单位给出两个地理坐标之间的距离DECLARE @g geography;DECLARE @h geography;SET @g = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326);SET @h = geography::STGeomFromText('POINT(-122.34900 47.65100)', 4326);SELECT @g.STDistance(@h);用法:select [dbo].[fnCalcDistanceKM](13.077085,80.262675,13.065701,80.258916)

慕容708150

Create Function [dbo].[DistanceKM]&nbsp;(&nbsp;&nbsp; &nbsp; &nbsp; @Lat1 Float(18),&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; @Lat2 Float(18),&nbsp;&nbsp; &nbsp; &nbsp; @Long1 Float(18),&nbsp;&nbsp; &nbsp; &nbsp; @Long2 Float(18))Returns Float(18)ASBegin&nbsp; &nbsp; &nbsp; Declare @R Float(8);&nbsp;&nbsp; &nbsp; &nbsp; Declare @dLat Float(18);&nbsp;&nbsp; &nbsp; &nbsp; Declare @dLon Float(18);&nbsp;&nbsp; &nbsp; &nbsp; Declare @a Float(18);&nbsp;&nbsp; &nbsp; &nbsp; Declare @c Float(18);&nbsp;&nbsp; &nbsp; &nbsp; Declare @d Float(18);&nbsp; &nbsp; &nbsp; Set @R =&nbsp; 6367.45&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --Miles 3956.55&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --Kilometers 6367.45&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --Feet 20890584&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --Meters 6367450&nbsp;&nbsp; &nbsp; &nbsp; Set @dLat = Radians(@lat2 - @lat1);&nbsp; &nbsp; &nbsp; Set @dLon = Radians(@long2 - @long1);&nbsp; &nbsp; &nbsp; Set @a = Sin(@dLat / 2)&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Sin(@dLat / 2)&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ Cos(Radians(@lat1))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Cos(Radians(@lat2))&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Sin(@dLon / 2)&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Sin(@dLon / 2);&nbsp;&nbsp; &nbsp; &nbsp; Set @c = 2 * Asin(Min(Sqrt(@a)));&nbsp;&nbsp; &nbsp; &nbsp; Set @d = @R * @c;&nbsp;&nbsp; &nbsp; &nbsp; Return @d;&nbsp;EndGO用法:选择dbo.DistanceKM(37.848832506474,37.848732506474,27.83935546875,27.83905546875)输出:0,02849639您可以使用带注释的浮点数更改@R参数。
打开App,查看更多内容
随时随地看视频慕课网APP