package com.hwariot.device.util;
import java.math.BigDecimal;
/**
* 根据给定经纬度和方位角以及距离计算新的经纬度
* @author qzz
* @date 2024/5/27
*/
public class GeoUtils {
public static final double EARTH_RADIUS = 6371; // 地球半径(km)
/**
* 根据给定经纬度和方位角以及距离计算新的经纬度
*/
public static double[] getDestinationPoint(double lat, double lon, double distance, double bearing, int precision) {
double angularDistance = distance / EARTH_RADIUS;
double bearingRad = Math.toRadians(bearing);
double latRad = Math.toRadians(lat);
double lonRad = Math.toRadians(lon);
double newLat = Math.asin(Math.sin(latRad) * Math.cos(angularDistance) +
Math.cos(latRad) * Math.sin(angularDistance) * Math.cos(bearingRad));
double newLon = lonRad + Math.atan2(Math.sin(bearingRad) * Math.sin(angularDistance) * Math.cos(latRad),
Math.cos(angularDistance) - Math.sin(latRad) * Math.sin(newLat));
double lat2 = Math.toDegrees(newLat);
double lon2 = Math.toDegrees(newLon);
// Round to specified precision
return new double[] {
roundToPrecision(lat2, precision),
roundToPrecision(lon2, precision)
};
}
private static double roundToPrecision(double value, int precision) {
BigDecimal bd = BigDecimal.valueOf(value);
bd = bd.setScale(precision, BigDecimal.ROUND_HALF_UP);
return bd.doubleValue();
}
// 示例用法
public static void main(String[] args) {
double lat1 = 0; // 点1纬度
double lon1 = 0; // 点1经度
double lat2 = 36; // 点2纬度
double lon2 = 36; // 点2经度
double distance = 2; // 以千米为单位
//计算方向角
double bearing1 = BearingCalculator.bearing(lat1, lon1, lat2, lon2);// 以度为单位
double bearing2 = Double.parseDouble("90") - bearing1;
//根据给定经纬度和方位角以及距离计算新的经纬度
double[] point12 = getDestinationPoint(lat1, lon1, distance, -bearing2, 6);
System.out.println("New latitude: " + point12[0]+","+point12[1]);
double[] point22 = getDestinationPoint(lat2, lon2, distance, -bearing1, 6);
System.out.println("New latitude: " + point22[0]+","+point22[1]);
}
}
根据给定经纬度和方位角以及距离计算新的经纬度
最新推荐文章于 2024-11-02 12:56:04 发布