java以速度在两个gps点之间进行插值

时间:2016-08-01 05:32:02

标签: java gps

我试图提出可以每秒填充两点之间的gps坐标的函数。这里有关于此的帖子很少,但我找不到完整的内容。我找到的最接近的答案是:
Interpolate between 2 GPS locations based on walking speed
我使用轴承修改了其中一个答案。但是,它似乎仍然无法奏效。特别是我认为距离计算是错误的。有人可以查看下面的代码并更改吗?
谢谢!

import java.util.ArrayList;

public class Test {

    double radius = 6371;

    public Test() {
        Location start = new Location(lat, lon);
        Location end = new Location(lat, lon);
        double speed = 1.39;
        double distance = CalculateDistanceBetweenLocations(start, end);
        double duration = distance / speed;
        System.out.println(distance + ", " + speed + ", " + duration);
        ArrayList<Location> locations = new ArrayList<Location>();
        for (double i = 0; i < duration; i += 1.0) {
            double bearing = CalculateBearing(start, end);
            double distanceInKm = speed / 1000;
            Location intermediaryLocation = CalculateDestinationLocation(start, bearing, distanceInKm);
            locations.add(intermediaryLocation);
            System.out.println(intermediaryLocation.latitude + ", " + intermediaryLocation.longitude);
            start = intermediaryLocation;
        }
    }

    double DegToRad(double deg) {
        return (deg * Math.PI / 180);
    }

    double RadToDeg(double rad) {
        return (rad * 180 / Math.PI);
    }

    double CalculateBearing(Location startPoint, Location endPoint) {
        double lat1 = DegToRad(startPoint.latitude);
        double lat2 = DegToRad(endPoint.latitude);
        double deltaLon = DegToRad(endPoint.longitude - startPoint.longitude);
        double y = Math.sin(deltaLon) * Math.cos(lat2);
        double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(deltaLon);
        double bearing = Math.atan2(y, x);
        return (RadToDeg(bearing) + 360) % 360;
    }

    Location CalculateDestinationLocation(Location point, double bearing, double distance) {
        distance = distance / radius;
        bearing = DegToRad(bearing);
        double lat1 = DegToRad(point.latitude);
        double lon1 = DegToRad(point.longitude);
        double lat2 = Math
                .asin(Math.sin(lat1) * Math.cos(distance) + Math.cos(lat1) * Math.sin(distance) * Math.cos(bearing));
        double lon2 = lon1 + Math.atan2(Math.sin(bearing) * Math.sin(distance) * Math.cos(lat1),
                Math.cos(distance) - Math.sin(lat1) * Math.sin(lat2));
        lon2 = (lon2 + 3 * Math.PI) % (2 * Math.PI) - Math.PI;
        return new Location(RadToDeg(lat2), RadToDeg(lon2));
    }

    double CalculateDistanceBetweenLocations(Location startPoint, Location endPoint) {
        double lat1 = DegToRad(startPoint.latitude);
        double lon1 = DegToRad(startPoint.longitude);
        double lat2 = DegToRad(endPoint.latitude);
        double lon2 = DegToRad(endPoint.longitude);
        double deltaLat = lat2 - lat1;
        double deltaLon = lon2 - lon1;
        double a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2)
                + Math.cos(lat1) * Math.cos(lat2) * Math.sin(deltaLon / 2) * Math.sin(deltaLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - 1));
        return (radius * c);
    }

    public static void main(String[] args) {
        new Test();
    }

    class Location {
        public double latitude, longitude;

        public Location(double lat, double lon) {
            latitude = lat;
            longitude = lon;
        }
    }

}

2 个答案:

答案 0 :(得分:0)

您的方法CalculateDistanceBetweenLocations包含以下一行:

double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - 1));

相当于

double c = 2 * Math.atan2(Math.sqrt(a), 0.0);

表示只要Math.atan2为正数,pi的结果始终为a,与a的值无关。

因此,CalculateDistanceBetweenLocations始终返回20015.086796020572,与输入坐标无关。

答案 1 :(得分:0)

您在行中输入错误

 double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - 1));

应该是

 double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));