| | |
| | | gcj02towgs84, |
| | | |
| | | //从GPS转高德 |
| | | wgs84_To_Gcj02 |
| | | wgs84_To_Gcj02, |
| | | |
| | | /** |
| | | * 计算一组经纬度坐标的中心点 |
| | | * @param {Array} coordinates - 经纬度数组,格式: [{lng: number, lat: number}, ...] |
| | | * @returns {Object} 中心点坐标 {lng: number, lat: number} |
| | | */ |
| | | calculateCenterCoordinates(coordinates) { |
| | | if (coordinates.length === 0) return { lng: 0, lat: 0 }; |
| | | if (coordinates.length === 1) return coordinates[0]; |
| | | |
| | | let x = 0, |
| | | y = 0, |
| | | z = 0; |
| | | coordinates.forEach((p) => { |
| | | const lng = (p.lng * Math.PI) / 180; // 经度转弧度 |
| | | const lat = (p.lat * Math.PI) / 180; // 纬度转弧度 |
| | | |
| | | // 转换为三维坐标 |
| | | x += Math.cos(lat) * Math.cos(lng); |
| | | y += Math.cos(lat) * Math.sin(lng); |
| | | z += Math.sin(lat); |
| | | }); |
| | | |
| | | // 取平均值 |
| | | const avgX = x / coordinates.length; |
| | | const avgY = y / coordinates.length; |
| | | const avgZ = z / coordinates.length; |
| | | |
| | | // 转换回经纬度 |
| | | const lng = (Math.atan2(avgY, avgX) * 180) / Math.PI; |
| | | const hypotenuse = Math.sqrt(avgX ** 2 + avgY ** 2); |
| | | const lat = (Math.atan2(avgZ, hypotenuse) * 180) / Math.PI; |
| | | |
| | | return { lng, lat }; |
| | | } |
| | | }; |