如何提高经纬度两个点之间的X距离(米)?

时间:2020-04-20 16:40:33

标签: java maps latitude-longitude

我正在用Java在不同的停靠点处模拟一条折线,两点之间的距离以米为单位,问题是,我必须以每米1至3米的速度从A点到B点其次,我需要每隔15分钟或多或少获取当前坐标,我该怎么做?

点之间的方式是直线,所有这些都是模拟的,不是在地图上或其他东西中发生的,我只需要每X次打印此信息,有什么帮助吗?

示例:

我有坐标:

LAT: 51.504870000000004 LNG: -0.21533000000000002

我必须以这样的速度去:

LAT: 51.50475 LNG: -0.21571

所以,我必须模拟一下我从A到B在3米处走动,并且我需要知道我在这两点之间移动时的位置(经/纬度)

还有一个question大致相同,不同之处在于我无法使用android做到这一点,它是Java应用程序。

2 个答案:

答案 0 :(得分:0)

我不清楚这个问题,但是如果您想在给定距离后做点什么,可以使用这种方法

LatLng startPoint=new LatLng(51.504870000000004,-0.21533000000000002);
LatLng endPoin=new LatLng(51.50475,-0.21571);
Location loc1 = new Location("");
loc1.setLatitude(startPoint.latitude);
loc1.setLongitude(startPoint.longitude);
Location loc2 = new Location("");
loc2.setLatitude(endPoint.latitude);
loc2.setLongitude(endPoint.longitude);
float distance = loc1.distanceTo(loc2);
//instead of if you can use for loop
if (distance <= 100) {
//do something, like get the coordinates
} else {
 //do something
}

答案 1 :(得分:0)

所以您知道latA, lngA, latB, lngB。根据您的问题,我假设您知道速度,它是恒定的,v = 3 m / s。您可以获取开始时间LocalDateTime tA = LocalDateTime.now();。您想在某个时刻tX知道自己的坐标。

为此,我将介绍coefLatcoefLng系数,用于将坐标转换为米并返回。他们使用地球的平均半径并将度数转换为弧度:

double coefLat = 180 / Math.PI / 6371000;
double coefLng = coefLat / Math.cos(Math.PI * (latA + latB) / 360);

然后通过纬度和经度轴计算距离以及以米为单位的完整距离:

double distLat = (latB - latA) / coefLat;
double distLng = (lngB - lngA) / coefLng;
double dist = Math.sqrt(distLat * distLat + distLng * distLng);

enter image description here

double fullTime = dist / v; // Full time needed to pass from A to B in seconds

移动一段时间后,找到持续时间并获取当前坐标:

LocalDateTime tX = LocalDateTime.now(); // get current time
long dT = Duration.between(tA, tX).getSeconds(); // in seconds
double ratio = dT / fullTime;

double latX = latA + coefLat * ratio * distLat;
double lngX = lngA + coefLng * ratio * distLng;

另请参阅this答案

完整代码:

public class GetCurrentCoords {

    public static void main(String[] args) {

        LocalDateTime tA = LocalDateTime.now();
        double latA = 51.504870000000004;
        double lngA = -0.21533000000000002;
        double latB = 51.50475;
        double lngB = -0.21571;

        double coefLat = 180 / Math.PI / 6371000;
        double coefLng = coefLat / Math.cos(Math.PI * (latA + latB) / 360);

        double distLat = (latB - latA) / coefLat; // meters
        double distLng = (lngB - lngA) / coefLng; // meters

        double dist = Math.sqrt(distLat * distLat + distLng * distLng);
        System.out.println("distLat = " + distLat + "m; distLng = " + distLng + "m; full dist from A to B = " + dist + "m");
        double v = 3;

        double fullTime = dist / v; // seconds
        System.out.println("full time from A to B = " + fullTime + "s");

        // let's move for 4 seconds
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException ex) {
            Logger.getLogger(GetCurrentCoords.class.getName()).log(Level.SEVERE, null, ex);
        }

        LocalDateTime tX = LocalDateTime.now(); 
        long dT = Duration.between(tA, tX).getSeconds();
        double ratio = dT / fullTime;

        double latX = latA + coefLat * ratio * distLat;
        double lngX = lngA + coefLng * ratio * distLng;

        System.out.println("Moving " + dT + " seconds; latX = " + latX + "; lngX = " + lngX);
     }
}
相关问题