WeChat applet calculates the distance between two coordinates in batches

Calculate the quasi-straight line distance of two latitude and longitude coordinate systems

  1. Get the latitude and longitude coordinates of the relative position
  2. Obtain the data latitude and longitude coordinate points of the corresponding batch statistics
  3. Mathematical method to calculate two straight line records
/*
	1.假设地球是一个完美的球体
	2.A坐标(x,y) B坐标(x,y)
	计算可得
	X轴差值:|A.x - B.x|
	Y轴差值:|A.y - B.y|
	模拟以量条计算所得的距离为直角坐标系原点无限延长的交点
	C边:X²+Y²=C²
	当坐标系周展示以球体的方式展示
*/
let MathLeb = Math.PI / 180.0;//经纬度转换成三角函数中度分表形式。

Coordinate description
Demo example

// 计算坐标距离
//pow() 方法可返回 x 的 y 次幂的值。
MapMath:async function(){
    
    
	let A = {
    
    "lat":102,"lng":100};
	let B = [{
    
    "lat":100,"lng":100},{
    
    "lat":101,"lng":101}];
	for(var i=0;i<B.length;i++){
    
    
		B[i].distance = await this.GetMapDistance(A.lat,A.lng,B[i].lat,B[i].lng);
	}
	console.log("B数组",B);
}
GetMapDistance:async function(lat1,lng1,lat2,lng2) {
    
    
let MathLeb = Math.PI / 180.0;//经纬度转换成三角函数中度分表形式。
   var radLat1 = Number(lat1*MathLeb);
   var radLat2 = Number(lat2*MathLeb);
   var a = radLat1 - radLat2;
   var  b = Number(lng1*MathLeb) - Number(lng2*MathLeb);
   var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
   Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
   s = s *6378.137 ;//地球半径;
   s = Math.round(s * 10000) / 10000;//以Km输出
   s=s.toFixed(2);
   return s;
}

Summary:
Basic Mathematical Method Right-
angled Triangle, Angle Calculation
This method is to calculate the value and the result set when the theoretical data is correct.
The specific use can be adjusted according to personal business needs.
If you don’t understand, you can leave a message and discuss together.
Mark!

Guess you like

Origin blog.csdn.net/qq_43784821/article/details/112781516