gpt4 book ai didi

java - 从给定纬度和经度的固定距离生成随机地理点

转载 作者:行者123 更新时间:2023-11-30 10:21:48 25 4
gpt4 key购买 nike

我正在编写一个 java 程序来生成所有经度和纬度与我给定点的固定距离。距离必须精确到 2000 公里,而不是不到 2000 公里。

这是我的代码

public static void getLocation(double x0, double y0, int meters) {
Random random = new Random();

// Convert radius from meters to degrees
double radiusInDegrees = meters / 111000f;

double u = random.nextDouble();
double v = random.nextDouble();
double w = radiusInDegrees * Math.sqrt(u);
double t = 2 * Math.PI * v;
double x = w * Math.cos(t);
double y = w * Math.sin(t);

// Adjust the x-coordinate for the shrinking of the east-west distances
// double new_x = x / Math.cos(Math.toRadians(y0));

double foundLongitude = x + x0;
double foundLatitude = y + y0;
System.out.println("Longitude: " + foundLongitude + " Latitude: " + foundLatitude );
}

如何让所有的点到geo point的距离都相等,比如围成一个圆圈?

最佳答案

public static void generatePoint(double latitude, double longitude, double distanceInMetres, double bearing) {
Random random = new Random();

//int bear = random.nextInt(360);
double brngRad = Math.toRadians(bearing);
double latRad = Math.toRadians(latitude);
double lonRad = Math.toRadians(longitude);
int earthRadiusInMetres = 6371000;
double distFrac = distanceInMetres / earthRadiusInMetres;

double latitudeResult = Math.asin(Math.sin(latRad) * Math.cos(distFrac) + Math.cos(latRad) * Math.sin(distFrac) * Math.cos(brngRad));
double a = Math.atan2(Math.sin(brngRad) * Math.sin(distFrac) * Math.cos(latRad), Math.cos(distFrac) - Math.sin(latRad) * Math.sin(latitudeResult));
double longitudeResult = (lonRad + a + 3 * Math.PI) % (2 * Math.PI) - Math.PI;

System.out.println("bearing: "+bearing+ ", latitude: " + Math.toDegrees(latitudeResult) + ", longitude: " + Math.toDegrees(longitudeResult));
}

需要加轴承

关于java - 从给定纬度和经度的固定距离生成随机地理点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47625549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com