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

MATLAB function to calculate distance between two coordinates (latitude and longitude)

如何使用 MATLAB R2015a 计算两个世界地图坐标(纬度和经度)之间的距离(以米为单位)?

如果您有权访问制图工具箱,那么 this 页面中描述的功能可能会对您有所帮助。

在这种情况下,您可以使用函数distance(LAT1,LON1,LAT2,LON2) to get the length (in degrees) of the great circle arc connecting both points. Then you can convert this to either km or miles using the deg2km, deg2nm or deg2sm

获得以公里为单位的距离的一个班轮将是:

deg2km(distance(lat1, lon1, lat2, lon2))

如果您无法访问 MATLAB 映射工具箱,那么一个简单的近似方法是使用 Haversine 公式。以下是 link 的摘录:

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.

Here 是一个 MATLAB 实现:

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