6

I'm working on an Android app that uses Geopoints and I want to determinate a Geopoint from another Geopoint, a distance (in any format) and a polar angle. For example, I want to get coordinates of a place 100 meters in the North-North-East (22,5 degres) of my location got by the GPS in my phone.

The only method I've found is Location.distanceBetween(...).

Yury
  • 20,618
  • 7
  • 58
  • 86

2 Answers2

6

Implementation for Android. This code is great for Unit Testing in your aplication:

public double radiansFromDegrees(double degrees)
{
    return degrees * (Math.PI/180.0);
}

public double degreesFromRadians(double radians)
{
    return radians * (180.0/Math.PI);
}

public Location locationFromLocation(Location fromLocation, double distance, double bearingDegrees)
{
    double distanceKm = distance / 1000.0;
    double distanceRadians = distanceKm / 6371.0;
    //6,371 = Earth's radius in km
    double bearingRadians = this.radiansFromDegrees(bearingDegrees);
    double fromLatRadians = this.radiansFromDegrees(fromLocation.getLatitude());
    double fromLonRadians = this.radiansFromDegrees(fromLocation.getLongitude());

    double toLatRadians = Math.asin( Math.sin(fromLatRadians) * Math.cos(distanceRadians)
                               + Math.cos(fromLatRadians) * Math.sin(distanceRadians) * Math.cos(bearingRadians) );

    double toLonRadians = fromLonRadians + Math.atan2(Math.sin(bearingRadians)
                                                 * Math.sin(distanceRadians) * Math.cos(fromLatRadians), Math.cos(distanceRadians)
                                                 - Math.sin(fromLatRadians) * Math.sin(toLatRadians));

    // adjust toLonRadians to be in the range -180 to +180...
    toLonRadians = ((toLonRadians + 3*Math.PI) % (2*Math.PI) ) - Math.PI;

    Location result = new Location(LocationManager.GPS_PROVIDER);
    result.setLatitude(this.degreesFromRadians(toLatRadians));
    result.setLongitude(this.degreesFromRadians(toLonRadians));
    return result;
}
knagode
  • 5,816
  • 5
  • 49
  • 65
1

Take a look at great-circle formulas: http://en.wikipedia.org/wiki/Great-circle_distance

This should give You some hints on how to calculate the distances.

For a point in a given distance and heading, check http://williams.best.vwh.net/avform.htm#LL Those formulas look quite complicated, but are easy to implement ;)

Black
  • 5,022
  • 2
  • 22
  • 37
  • I implemented this for a project at work. Therefore, I am sorry to say that I cannot share the code. – Black Aug 05 '13 at 08:30
  • I already shared my code below ... I hope that will save some time to further users ;) – knagode Aug 05 '13 at 09:11