Method of calculating the distance between two points based on latitude and longitude

#include <iostream>
#include <cmath>

using namespace std;
#define pi 3.1415926535897932384626433832795
#define EARTH_RADIUS 6378.137 //地球半径 KM

double rad(double d)
{
    
    
	return d * pi / 180.0;
}
double RealDistance(double lat1, double lng1, double lat2, double lng2)//lat1第一个点纬度,lng1第一个点经度,lat2第二个点纬度,lng2第二个点经度
{
    
    
	double a;
	double b;
	double radLat1 = rad(lat1);
	double radLat2 = rad(lat2);
	a = radLat1 - radLat2;
	b = rad(lng1) - rad(lng2);
	double s = 2 * asin(sqrt(pow(sin(a / 2), 2) + cos(radLat1)*cos(radLat2)*pow(sin(b / 2), 2)));
	s = s * EARTH_RADIUS;
	s = s * 1000;
	return s;
}

int main()
{
    
    
	cout << RealDistance(20.194970489552652, 110.564224161560745, 20.195110378886003, 110.564232333140893) << std::endl;
	
	getchar();
	return 0;
}

Note: The above method is for angle calculation, if it is radians, it is as follows:

RealDistance(lat1, lng1, lat2, lng2) * 180 / pi;

The ocean unit knot is converted into meters/second:
1 knot is equal to 1 nautical mile per hour, which is 1.852 kilometers (km) per hour, 20 knots is 20 nautical miles/hour, 37.04 kilometers/hour, (speed unit), which is 10.28 M/s, 20 knots running for 100ms, you can move 10.28*0.1=1.028 meters! ! ! ,Run for three seconds, can move 30.84 meters

The corresponding executable program is as follows:
https://download.csdn.net/download/qq_23350817/15599651

Guess you like

Origin blog.csdn.net/qq_23350817/article/details/114390518