且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MATLAB函数可计算两个坐标(纬度和经度)之间的距离

更新时间:2022-12-09 12:55:37

如果您无权访问MATLAB映射工具箱,则一个简单的近似方法是使用

If you don't have access to the MATLAB Mapping toolbox then a simple approximation is to use the Haversine formula. Here is an excerpt from the link:

haversine公式是一个在导航中很重要的方程式,它给出了球面上两个点之间的经度和纬度之间的大圆距离.这是球面三角法中更通用的公式的特例,这是正弦曲线的定律,它关系到球面三角形的边和角.

The haversine formula is an equation important in navigation, giving great-circle distances between two points on a sphere from their longitudes and latitudes. It is a special case of a more general formula in spherical trigonometry, the law of haversines, relating the sides and angles of spherical triangles.

此处 是MATLAB实现:

Here is a MATLAB implementation:

function rad = radians(degree) 
% degrees to radians
    rad = degree .* pi / 180;
end; 

function [a,c,dlat,dlon]=haversine(lat1,lon1,lat2,lon2)
% HAVERSINE_FORMULA.AWK - converted from AWK 
    dlat = radians(lat2-lat1);
    dlon = radians(lon2-lon1);
    lat1 = radians(lat1);
    lat2 = radians(lat2);
    a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .* (sin(dlon./2)).^2;
    c = 2 .* asin(sqrt(a));
    arrayfun(@(x) printf("distance: %.4f km\n",6372.8 * x), c);
end;

[a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40); % BNA to LAX