Knowing the latitude and longitude of the two places, calculate the straight-line distance between the two places

1 Principle formula

On Earth, calculating the straight-line distance between two points usually uses a geographic coordinate system (such as WGS84). The formula for calculating the straight-line distance between two places is based on the Great Circle Distance between latitude and longitude. The formula is based on spherical trigonometry, and the commonly used formula is Haversine HaversineH avers in e official . _

Form 1:
dlon = lon 2 − lon 1 d_{lon} = lon_2 - lon_1dlon=lon2lon1
dlat = lat 2 − lat 1 d_{lat} = lat_2 - lat_1dlat=lat2lat1
a = s i n 2 ( d l a t / 2 ) + c o s ( l a t 1 ) ∗ c o s ( l a t 2 ) ∗ s i n 2 ( d l o n / 2 ) a = sin²(d_{lat}/2) + cos(lat_1) * cos(lat_2) * sin²(d_{lon}/2) a=sin2(dlat/2)+cos ( l to t1)cos ( l to t2)sin2(dlon/2)
c = 2 ∗ a t a n 2 ( a , ( 1 − a ) ) c = 2 * atan^2(\sqrt{a}, \sqrt{(1-a)}) c=2a t a n2(a ,(1a) )
d = R ∗ c d = R * c d=Rc

形式二:
d = R ∗ a c o s ( s i n ( l o n 1 ) ∗ s i n ( l o n 2 ) + c o s ( l o n 1 ) ∗ c o s ( l o n 2 ) ∗ c o s ( l a t 2 − l a t 1 ) ) d = R*acos(sin(lon_1)*sin(lon_2) + cos(lon_1)*cos(lon_2)*cos(lat_2-lat_1)) d=Racos(sin(lon1)sin(lon2)+cos(lon1)cos(lon2)cos ( l to t2lat1))

in:

  • R R R is the radius of the earth, which is about 6371 kilometers.

insert image description here

Note that this formula assumes the Earth is a perfect sphere. In reality, the shape of the Earth is more like an ellipsoid, so using more precise geographic information system (GIS) software or libraries such as proj.4 or GeographicLib may give more accurate results.

Also, latitude and longitude are usually measured in degrees, but the angles in the above formula should be treated as radians. If latitude and longitude are given in degrees, they need to be converted to radians. The radian value can be obtained by multiplying degrees by π/180 and taking the integer part. For example, 30° 30°30° converted to radians isπ / 180 ∗ 30 π/180*30p /18030

2 code implementation

2.1 JavaScript

function calculateDistance(lat1, lon1, lat2, lon2) {
    
    
  const R = 6371; // 地球半径,单位为千米
  const rad = (angle) => angle * Math.PI / 180; // 将角度转换为弧度

  const lat1Rad = rad(lat1);
  const lon1Rad = rad(lon1);
  const lat2Rad = rad(lat2);
  const lon2Rad = rad(lon2);

  const dLat = lat2Rad - lat1Rad;
  const dLon = lon2Rad - lon1Rad;

  const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(lat1Rad) * Math.cos(lat2Rad) *
    Math.sin(dLon / 2) * Math.sin(dLon / 2);
  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

  const distance = R * c; // 返回单位为千米的距离
  return distance;
}

// 示例用法
const lat1 = 39.9087; // 北京的经纬度
const lon1 = 116.4074;
const lat2 = 31.2304; // 上海的经纬度
const lon2 = 121.4737;

const distance = calculateDistance(lat1, lon1, lat2, lon2);
console.log(distance); // 输出直线距离(单位:千米)

2.2 C++

#include <cmath>
#include <iostream>

// 计算两个经纬度之间的距离(单位:千米)
double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
    
    
  const double R = 6371; // 地球半径,单位为千米

  // 将经纬度转换为弧度
  double lat1Rad = std::atan(std::tan(lat1 * (M_PI / 180)) * std::cos(lon1 * (M_PI / 180)));
  double lon1Rad = lon1 * (M_PI / 180);
  double lat2Rad = std::atan(std::tan(lat2 * (M_PI / 180)) * std::cos(lon2 * (M_PI / 180)));
  double lon2Rad = lon2 * (M_PI / 180);

  // 计算两个经纬度之间的弧度差
  double dLat = lat2Rad - lat1Rad;
  double dLon = lon2Rad - lon1Rad;

  // 根据球面三角法公式计算距离
  double a = std::sin(dLat / 2) * std::sin(dLat / 2) +
    std::cos(lat1Rad) * std::cos(lat2Rad) *
    std::sin(dLon / 2) * std::sin(dLon / 2);
  double c = 2 * std::atan2(std::sqrt(a), std::sqrt(1 - a));

  // 返回距离
  return R * c;
}

// 示例用法
int main() {
    
    
  double lat1 = 39.9087; // 北京的经纬度
  double lon1 = 116.4074;
  double lat2 = 31.2304; // 上海的经纬度
  double lon2 = 121.4737;

  double distance = calculateDistance(lat1, lon1, lat2, lon2);
  std::cout << "距离:" << distance << " 千米" << std::endl;

  return 0;
}

Note that this code uses math functions and constants from the C++ standard library. Also, the method of converting latitude and longitude to radians requires the use of the std::atan and std::tan functions.

2.3 Python

import math

def calculate_distance(lat1, lon1, lat2, lon2):
    R = 6371  # 地球半径,单位为千米
    rad = lambda angle: angle * math.pi / 180  # 将角度转换为弧度

    lat1_rad = rad(lat1)
    lon1_rad = rad(lon1)
    lat2_rad = rad(lat2)
    lon2_rad = rad(lon2)

    dLat = lat2_rad - lat1_rad
    dLon = lon2_rad - lon1_rad

    a = math.sin(dLat / 2) ** 2 + math.cos(lat1_rad) * math.cos(lat2_rad) * math.sin(dLon / 2) ** 2
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))

    distance = R * c  # 返回单位为千米的距离
    return distance

# 示例用法
lat1 = 39.9087  # 北京的经纬度
lon1 = 116.4074
lat2 = 31.2304  # 上海的经纬度
lon2 = 121.4737

distance = calculate_distance(lat1, lon1, lat2, lon2)
print(distance)  # 输出直线距离(单位:千米)

Note that due to some syntax differences between Python and JavaScript, the math module is required to perform mathematical calculations. Also, since there is no functionality for lambda functions in Python, you need to use ordinary function definitions instead.

2.4 MATLAB

function distance = calculateDistance(lat1, lon1, lat2, lon2)
    const R = 6371; % 地球半径,单位为千米

    lat1Rad = rad(lat1);
    lon1Rad = rad(lon1);
    lat2Rad = rad(lat2);
    lon2Rad = rad(lon2);

    dLat = lat2Rad - lat1Rad;
    dLon = lon2Rad - lon1Rad;

    a = sin(dLat / 2) .^ 2 + cos(lat1Rad) .* cos(lat2Rad) .* sin(dLon / 2) .^ 2;
    c = 2 * atan2(sqrt(a), sqrt(1 - a));

    distance = R * c; % 返回单位为千米的距离
end

% 示例用法
lat1 = 39.9087; % 北京的经纬度
lon1 = 116.4074;
lat2 = 31.2304; % 上海的经纬度
lon2 = 121.4737;

distance = calculateDistance(lat1, lon1, lat2, lon2);
disp(distance); % 输出直线距离(单位:千米)

Please note that the function definition in MATLAB starts with function, the input parameters are separated by commas, and the output results are returned using variable names. In addition, .* is used in MATLAB to represent the multiplication between elements, while ^ represents the power. Finally, use the disp function to output the result.

Guess you like

Origin blog.csdn.net/weixin_46098577/article/details/132553284