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
Let
and
be the geographical latitude and longitude of two points 1 and 2, and
their absolute differences; then
, the central angle between them, is given by the spherical law of cosines:
The distance d, i.e. the arc length, for a sphere of radius r and
given in radians
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]
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]
When programming a computer, one should use the atan2() function rather than the ordinary arctangent function (atan()), so that
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系列之如何获取地球上两经纬度坐标点间的距离的更多相关文章
- 计算地球上两个坐标点(经度,纬度)之间距离sql函数
go --计算地球上两个坐标点(经度,纬度)之间距离sql函数 --作者:lordbaby --整理:www.aspbc.com CREATE FUNCTION [dbo].[fnGetDistanc ...
- 获取经纬度之间两点间真实距离(适用于GoogleMap,BaiduMap,Amap等)
如何获取经纬度之间两点间真实距离(适用于GoogleMap,BaiduMap,Amap等) 目标:使用百度定位sdk开发实时移动距离计算功能,根据经纬度的定位,计算行驶公里数并实时刷新界面显示.大家 ...
- php根据地球上任意两点的经纬度计算两点间的距离 原理
地球是一个近乎标准的椭球体,它的赤道半径为6378.140千米,极半径为6356.755千米,平均半径6371.004千米.如果我们假设地球是一个完美的球体,那么它的半径就是地球的平均半径,记为R.如 ...
- C# 获取两点(经纬度表示)间的距离
#region 获取两点(经纬度表示)间的距离 /// <summary> /// 获取两点(经纬度表示)间的距离 /// </summary> /// <param n ...
- 用户Ip地址和百度地图api接口获取用户地理位置(经纬度坐标,城市)
<?php //获取用户ip(外网ip 服务器上可以获取用户外网Ip 本机ip地址只能获取127.0.0.1) function getip(){ if(!empty($_SERVE ...
- matlab练习程序(地图上画经纬度)
需要看下生成的数据在地球上的经纬度具体位置. 投影为墨卡托投影. clear all; close all; clc; load coast; a=load('out.txt'); %自己的经纬度 ...
- C#开发BIMFACE系列8 服务端API之获取文件上传状态信息
系列目录 [已更新最新开发文章,点击查看详细] 在BIMFACE控制台上传文件,上传过程及结束后它会自动告诉你文件的上传状态,目前有三种状态:uploading,success,failure ...
- Hadoop系列004-Hadoop运行模式(上)
title: Hadoop系列004-Hadoop运行模式(上) date: 2018-11-20 14:27:00 updated: 2018-11-20 14:27:00 categories: ...
- 【ABAP系列】SAP 获取工单和工序的状态
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 获取工单和工序的状态 ...
随机推荐
- 另类方法解决设计Web页面出现:Error Creating Control
在B/S开发的过程中,经常会遇到这样的提示:Error Creating Control ,而这些页面明明之前是可以打开的,但还是出现如下图所示: 网上找到的方法是把控件初始化放在OnInit里去写, ...
- BZOJ 4636 蒟蒻的数列
二分写错了血T..... 线段树标记永久化. #include<iostream> #include<cstdio> #include<cstring> #incl ...
- HDU 2577 How to Type (DP,经典)
题意: 打字游戏,求所按的最少次数.给出一个串,其中有大小写,大写需要按下cap键切换到大写,或者在小写状态下按shift+键,这样算两次,打小写时则相反.注意:在打完所有字后,如果cap键是开着的, ...
- UVA 489 Hangman Judge (字符匹配)
题意:给一个字符串A,只含小写字符数个.再给一个字符串B,含小写字符数个.规则如下: 1.字符串B从左至右逐个字符遍历,对于每个字符,如果该字符在A中存在,将A中所有该字符删掉,若不存在,则错误次数+ ...
- 【英语】Bingo口语笔记(5) - 英式和美式英语的发音区别
- 内存泄露(OOM)现象及举例
一.HeapSize OOM(堆空间内存溢出) A.eg:List.add(" ")在一个死循环中不断的调用add却没有remove. B.并发导致. 解决方法有:1.代码提速.这 ...
- mysql 添加索引后 在查询的时候是mysql就自动从索引里面查询了。还是查询的时候有单 独的参数查询索引?
MYSQL在创建索引后对索引的使用方式分为两种:1 由数据库的查询优化器自动判断是否使用索引:2 用户可在写SQL语句时强制使用索引 下面就两种索引使用方式进行说明第一种,自动使用索引.数据库在收到查 ...
- jQuery Mobile中文手册:开发入门
jQuery Mobile 以“Write Less, Do More”作为目标,为所有的主流移动操作系统平台提供了高度统一的 UI 框架:jQuery 的移动框架可以让你为所有流行的移动平台设计一个 ...
- javascript中createTextRange用法(focus)
createtextrange createrange区别: 对象或元素不同,虽然都是返回TextRange.例如: var r=document.body.createTextRange() ...
- linux下的加减运算
命令: echo rep 如果你有些编程方面的概念,最有意思的是用gdb,它可以进行进制转换,浮点数运算,数据类型占用字节数等等,很方便. (gdb) p 2+5$21 = 7(gdb) p 2/5$ ...



