C# JackLib系列之如何获取地球上两经纬度坐标点间的距离

时间:2022-05-22 04:45:09

获取地球上两经纬度坐标点间的距离,利用【大圆距离公式】

C# JackLib系列之如何获取地球上两经纬度坐标点间的距离

 

A diagram illustrating great-circle distance (drawn in red) between two points on a sphere, P and Q. Two antipodal points, u and v, are also depicted.

谷歌都在用呢, C#实现的代码如下:

 /// <summary>
/// 地球半径
/// </summary>
private const double EARTH_RADIUS = 6378.137;
/// <summary>
/// 获取两点之间的距离,大圆距离公式
/// </summary>
/// <param name="lat1"></param>
/// <param name="lon1"></param>
/// <param name="lat2"></param>
/// <param name="lon2"></param>
/// <returns></returns>
public static double DistanceOfEarthTwoPoints(double latA, double lngA, double latB, double lngB) {
double radLat1 = lat1 * Math.PI / 180.0;
     double radLat2 = lat2 * Math.PI / 180.0;
     double a = radLat1 - radLat2;
     double b = lon1 * Math.PI / 180.0 - lon2 * Math.PI / 180.0;
     double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));
     s = s * EARTH_RADIUS;
     s = Math.Round(s * 1000000) / 1000000;
     return s;
}

当然还有另一种写法:

/// <summary>
/// 获取两点之间的距离,大圆距离公式
/// </summary>
/// <param name="lat1"></param>
/// <param name="lon1"></param>
/// <param name="lat2"></param>
/// <param name="lon2"></param>
/// <returns></returns>
public static double DistanceOfEarthTwoPoints(double latA, double lngA, double latB, double lngB) {
double s = Math.Acos(Math.Cos(Rad(latA)) * Math.Cos(Rad(latB)) * (Math.Cos(Rad(lngA) - Rad(lngB))) + Math.Sin(Rad(latA)) * Math.Sin(Rad(latB)));
s = s * EARTH_RADIUS;
s = Math.Round(s * ) / ;
return s;
}

其实这两个方法是完全等价的,只是化简程序不同而已,看看下面的解释:

Formulas

C# JackLib系列之如何获取地球上两经纬度坐标点间的距离
 
An illustration of the central angle, Δσ, between two points, P and Q. λ and φ are the longitudinal and latitudinal angles of P respectively

Let C# JackLib系列之如何获取地球上两经纬度坐标点间的距离 and C# JackLib系列之如何获取地球上两经纬度坐标点间的距离 be the geographical latitude and longitude of two points 1 and 2, and C# JackLib系列之如何获取地球上两经纬度坐标点间的距离 their absolute differences; then C# JackLib系列之如何获取地球上两经纬度坐标点间的距离, the central angle between them, is given by the spherical law of cosines:

C# JackLib系列之如何获取地球上两经纬度坐标点间的距离

The distance d, i.e. the arc length, for a sphere of radius r and C# JackLib系列之如何获取地球上两经纬度坐标点间的距离 given in radians

C# JackLib系列之如何获取地球上两经纬度坐标点间的距离

Computational formulas

On computer systems with low floating-point precision, the spherical law of cosines formula can have large rounding errors if the distance is small (if the two points are a kilometer apart on the surface of the Earth, the cosine of the central angle comes out 0.99999999). For modern 64-bit floating-point numbers, the spherical law of cosines formula, given above, does not have serious rounding errors for distances larger than a few meters on the surface of the Earth.[2] The haversine formula is numerically better-conditioned for small distances:[3]

C# JackLib系列之如何获取地球上两经纬度坐标点间的距离

Historically, the use of this formula was simplified by the availability of tables for the haversine function: hav(θ) = sin2(θ/2).

Although this formula is accurate for most distances on a sphere, it too suffers from rounding errors for the special (and somewhat unusual) case of antipodal points (on opposite ends of the sphere). A more complicated formula that is accurate for all distances is the following special case of the Vincenty formula for an ellipsoid with equal major and minor axes:[4]

C# JackLib系列之如何获取地球上两经纬度坐标点间的距离

When programming a computer, one should use the atan2() function rather than the ordinary arctangent function (atan()), so that C# JackLib系列之如何获取地球上两经纬度坐标点间的距离 is placed in the correct quadrant.

The determination of the great-circle distance is just part of the more general problem of great-circle navigation, which also computes the azimuths at the end points and intermediate way-points.

C# JackLib系列之如何获取地球上两经纬度坐标点间的距离的更多相关文章

  1. 计算地球上两个坐标点(经度,纬度)之间距离sql函数

    go --计算地球上两个坐标点(经度,纬度)之间距离sql函数 --作者:lordbaby --整理:www.aspbc.com CREATE FUNCTION [dbo].[fnGetDistanc ...

  2. 获取经纬度之间两点间真实距离(适用于GoogleMap&comma;BaiduMap&comma;Amap等)

    如何获取经纬度之间两点间真实距离(适用于GoogleMap,BaiduMap,Amap等)  目标:使用百度定位sdk开发实时移动距离计算功能,根据经纬度的定位,计算行驶公里数并实时刷新界面显示.大家 ...

  3. php根据地球上任意两点的经纬度计算两点间的距离 原理

    地球是一个近乎标准的椭球体,它的赤道半径为6378.140千米,极半径为6356.755千米,平均半径6371.004千米.如果我们假设地球是一个完美的球体,那么它的半径就是地球的平均半径,记为R.如 ...

  4. C&num; 获取两点(经纬度表示)间的距离

    #region 获取两点(经纬度表示)间的距离 /// <summary> /// 获取两点(经纬度表示)间的距离 /// </summary> /// <param n ...

  5. 用户Ip地址和百度地图api接口获取用户地理位置(经纬度坐标,城市)

    <?php   //获取用户ip(外网ip 服务器上可以获取用户外网Ip 本机ip地址只能获取127.0.0.1) function getip(){     if(!empty($_SERVE ...

  6. matlab练习程序(地图上画经纬度)

    需要看下生成的数据在地球上的经纬度具体位置. 投影为墨卡托投影.   clear all; close all; clc; load coast; a=load('out.txt'); %自己的经纬度 ...

  7. C&num;开发BIMFACE系列8 服务端API之获取文件上传状态信息

    系列目录     [已更新最新开发文章,点击查看详细] 在BIMFACE控制台上传文件,上传过程及结束后它会自动告诉你文件的上传状态,目前有三种状态:uploading,success,failure ...

  8. Hadoop系列004-Hadoop运行模式(上)

    title: Hadoop系列004-Hadoop运行模式(上) date: 2018-11-20 14:27:00 updated: 2018-11-20 14:27:00 categories: ...

  9. 【ABAP系列】SAP 获取工单和工序的状态

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 获取工单和工序的状态   ...

随机推荐

  1. Ubuntu 下 kazam 录屏 没声音解决方案

    以下内容参考https://www.youtube.com/watch?v=5NZ0qwp2L04,我做了些修改,让它好懂些. 在应用商店里搜索 PulseAudio Volume Control 在 ...

  2. cas与NGINX整合(转)

    CAS (5) -- Nginx代理模式下浏览器访问CAS服务器配置详解 标签: 服务器 2015-12-18 15:04 1633人阅读 评论(0) 收藏 举报  分类: 网络(61)    目录( ...

  3. linux&lowbar;目录结构

    目录的作用是什么? 1. 归档和分类 2. 区分同名文件 什么是FHS? 目录层次标准,linux目录规范标准 linux系统目录有哪些特点? 1. 逻辑上所有目录都在 / 目录下,根目录是所有目录的 ...

  4. 00&lowbar;HTML入门第二天

    PS切图 快捷键操作无效,原因是没有切换到英文输入状态 常用快捷键新建 CTRL+N       打开 CTRL+O关闭 CTRL+W保存 CTRL+S 另存为 CTRL+SHIFT+S       ...

  5. PowerShell 发布farm solution

    SharePoint PowerShell在SharePoint Product列表里边,然后以管理员权限启动. 1. 添加Solution 到 SharePoint Farm. Add-SPSolu ...

  6. Javascript书籍推荐----&lpar;步步为赢&rpar;

    在此分享一些高清javascript书籍,因为我也没有全部看完,所以在这只是推荐,不同的书适合不同的人,所有的书在网上均有电子书,若找不到,请在博客留言,我有大部分书籍的电子稿.希望有更多的好书分享出 ...

  7. &lt&semi;airsim文档学习&gt&semi; Street View Image&comma; Pose&comma; and 3D Cities Dataset

    原文地址:  https://github.com/amir32002/3D_Street_View 说明:个人学习笔记,翻译整理自github/airsim. 简介 该存储库共享包含6DOF相机姿态 ...

  8. css3火焰文字样式代码

    css样式: <style type="text/css"> body{background:#000;} *{margin:0;padding:0;transitio ...

  9. stark - filter、pop、总结

    一.filter 效果图 知识点 1.配置得显示Filter,不配置就不显示了 list_filter = ['title','publish', 'authors'] 2.前端显示 后端返回 字典 ...

  10. Android Fragment重要函数

    Fragment的常用函数: 一.Fragment对象 1.void setArguments(Bundle args); 这个函数为Fragment提供构造参数(也就是数据),参数以Bundle类型 ...