javascript - javascript函数和sql server STD距离计算的距离差异

标签 javascript sql-server-2012 geospatial

DECLARE @orig geography = geography::Point(17, 78, 4326);     
Select  @orig.STDistance(geography::Point(17.001, 78.00001, 4326))/1000

给出

110.674385214845

function calculateDistance(lat1, lon1, lat2, lon2, units) {
        var R = 6371; // Radius of the earth in km
        var dLat = deg2rad(lat2 - lat1);  // deg2rad below
        var dLon = deg2rad(lon2 - lon1);
        var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
        Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
        Math.sin(dLon / 2) * Math.sin(dLon / 2);
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        var d = R * c; // Distance in km
        return d;
}
function deg2rad(deg) {
        return deg * (Math.PI / 180)
}`

给出 0.11120001099379416公里

相差 0.6 米

最佳答案

原因是因为Haversine formula您上面使用的假设是一个球形地球,周长为 6371 公里,而地理数据类型使用 WGS 84 ellipsoid 。两者之间的主要区别在于,在 WGS 84 中,地球是扁球体(即,在两极被压扁),并且假设赤道周围的周长为 6378.13 公里,但两极周围的周长为 6356.75 公里。使用 WGS 84 椭球体进行计算的完整方程相当复杂,在大多数情况下半正弦被认为是一个很好的近似值。

如果您查看这个优秀资源的介绍,http://www.movable-type.co.uk/scripts/latlong.html ,作者明确指出了这一点,并建议忽略椭球体的误差平均约为 0.3%(你的约为 0.4%)。我在这里重复作者的引言,以防万一页面出现故障。

All these formulæ are for calculations on the basis of a spherical earth (ignoring ellipsoidal effects) – which is accurate enough* for most purposes… [In fact, the earth is very slightly ellipsoidal; using a spherical model gives errors typically up to 0.3% – see notes for further details].

关于javascript - javascript函数和sql server STD距离计算的距离差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25422857/

相关文章:

javascript - 更新或更改或删除/重置 Javascript 事件监听器

sql-server - 附加sql server 2012错误,无法打开物理文件

sql-server - SELECT DISTINCT 显示每个客户电子邮件的重复日期

MongoDB 地理空间查询领域重叠单点

javascript - 在运行 frisby 测试之前,将数据存储在 var 中,以便在 REST API 路由中作为参数使用

javascript - 文本替换为js(保留uc)

mysql - 给定经纬度,找到对该位置感兴趣的用户

mysql - 从MySQL中的多点数据类型获取点数组(lat,lng)

javascript - Lodash orderBy - 在开头以 $ 符号对价格进行排序,如数字

symfony - 文档 :mapping:import for Symfony2 中未知的数据库类型几何请求错误