从给定的纬度和经度生成一个固定距离的随机地理坐标点

3

我正在编写一个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 );
    } 

我该如何使所有点与地理点等距离生成,就像形成一个圆圈一样?

假设我们有一个带有坐标X、Y和半径R的地理点。你想要获取整个圆周上的点的坐标,对吗? - Z.R.T.
yupppp........... - user2399158
随机选择一个方位角,然后使用距离和方位角公式计算目标纬度/经度。这个答案提供了一种解决方案。更多细节请参见此处 - teppic
感谢。 - user2399158
2个回答

1
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));
    }

需要添加轴承。

1

对于任何需要的人,这是我如何在JavaScript中实现此代码:

function generatePoint(
  latitude,
  longitude,
  distanceInMetres,
  bearing = Math.floor(Math.random() * (360 - 1) + 1)
) {
  const brngRad = deg2Rad(bearing);
  const latRad = deg2Rad(latitude);
  const lonRad = deg2Rad(longitude);

  const EARTH_RADIUS_IN_METRES = 6371000;

  const distFrac = distanceInMetres / EARTH_RADIUS_IN_METRES;

  const latitudeResult = Math.asin(
    Math.sin(latRad) * Math.cos(distFrac) +
      Math.cos(latRad) * Math.sin(distFrac) * Math.cos(brngRad)
  );
  const a = Math.atan2(
    Math.sin(brngRad) * Math.sin(distFrac) * Math.cos(latRad),
    Math.cos(distFrac) - Math.sin(latRad) * Math.sin(latitudeResult)
  );

  const longitudeResult =
    ((lonRad + a + 3 * Math.PI) % (2 * Math.PI)) - Math.PI;

  return {
    latitude: rad2Deg(latitudeResult),
    longitude: rad2Deg(longitudeResult),
    bearing,
  };
}

function deg2Rad(deg) {
  return deg * (Math.PI / 180);
}

function rad2Deg(rad) {
  return rad * (180 / Math.PI);
}

如果没有提供方向,它还会生成一个随机的方向。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接