TheQ
TheQ

Reputation: 2019

Calculate new gps coordinates from 1 set of coordinates and distance?

so I was wondering if there was existing code/library that allowed me to calculate a new pair of gps coordinates given: distance, a pair of latitude and longituide and bearing.

I hope you guys can give me insight.

cheers!

Upvotes: 0

Views: 1366

Answers (1)

TheQ
TheQ

Reputation: 2019

The answer that was given to me was from this link: stackoverflow.com/questions/37348207/find-location-1-km-away-in-180-degree-south-from-my-location/37349396#37349396

Using the following library: https://github.com/JavadocMD/simplelatlng But the method that does the calculation is below:

/**
     * <p>
     * Calculate the end point of traveling along a great-circle path from a
     * given starting point with a given intitial bearing for a known distance.
     * </p>
     * 
     * @param start
     *            the starting point.
     * @param initialBearing
     *            the initial bearing.
     * @param distance
     *            the distance to travel.
     * @param unit
     *            the unit in which distance is measured.
     * @return the end point.
     */
    public static LatLng travel(LatLng start, double initialBearing, double distance,
            LengthUnit unit) {
        double bR = Math.toRadians(initialBearing);
        double lat1R = Math.toRadians(start.getLatitude());
        double lon1R = Math.toRadians(start.getLongitude());
        double dR = distance / LatLngConfig.getEarthRadius(unit);

        double a = Math.sin(dR) * Math.cos(lat1R);
        double lat2 = Math.asin(Math.sin(lat1R) * Math.cos(dR) + a * Math.cos(bR));
        double lon2 = lon1R
                + Math.atan2(Math.sin(bR) * a, Math.cos(dR) - Math.sin(lat1R) * Math.sin(lat2));
        return new LatLng(Math.toDegrees(lat2), Math.toDegrees(lon2));
    }

Upvotes: 4

Related Questions