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

标签 java

我正在编写一个 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/

相关文章:

java - 如何调试JSTL?

java - 无法在 Azure RHEL 节点中使用 yum 访问第三方存储库

java - 如何解决连接设备android studio中显示的模拟器超过一个的问题

java - 通过类变量调用时,Java 9 中的 Class.cast 和 Class.isInstance 方法实际上是固有的吗?

java - Eclipse:如何将当前时间写入文件作为每个构建的一部分?

java - 按标题查找窗口并在Java中置于前台

java - 如何将一个数组列表中的数据共享给其他几个数组列表

多个参数的Java流averagingInt

java - API 中的部分更新

java - 如何在 Hibernate 3.6 中正确级联保存主键上的一对一双向关系