简体   繁体   English

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

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

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

If you don't have access to the MATLAB Mapping toolbox then a simple approximation is to use the Haversine formula. 如果您无权访问MATLAB映射工具箱,则一个简单的近似方法是使用Haversine公式。 Here is an excerpt from the 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. Haversine公式是在导航中很重要的方程式,它可以根据其经度和纬度给出球面上两个点之间的大圆距离。 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 is a MATLAB implementation: 是一个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

If you have access to Mapping toolbox then may be function described on this page could help you. 如果您有权访问“映射”工具箱,则页面上描述的功能可能会对您有所帮助。

In that case, you can use the function distance(LAT1,LON1,LAT2,LON2) to get the length (in degrees) of the great circle arc connecting both points. 在这种情况下,可以使用函数distance(LAT1,LON1,LAT2,LON2)来获取连接两个点的大圆弧的长度(以度为单位)。 Then you can convert this to either km or miles using the deg2km , deg2nm or deg2sm . 然后,您可以使用deg2kmdeg2nmdeg2sm将其转换为km或miles。

A one liner to get the distance in km would be: 一个以千米为单位的距离的班轮是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM