views:

212

answers:

4

I have a location with latitude and longitude and want to get a new location that has a distance of x meters from that location at an angle of d degrees. This would be the reverse of Location.distanceBetween(). Is there any Android API to do that. I know that I could program such a function myself, but I wonder if there is an API for it already.

+1  A: 

It doesn't look like it.

CaseyB
A: 

Implement this yourself for now, but do expect this function to show up at some point, so code accordingly - create your own function, add a few unit tests.

In the future add the following to you function:

def myFunc(args):
  res = # compute stuff
  #if(debug):
    res2 = # make api call
    assert(res = res2)
  return res

And some time later:

def myFunc(args):
  return # make api call

And some time later remove the function altogether.

Hamish Grubijan
+1  A: 

This is called the "first geodesic" (or sometimes "principal geodesic") problem, which will probably help you in finding an algorithm on Google if you need to implement this yourself.

John Feminella
+3  A: 

There are some formulae and sample code (JavaScript) for this here: Movable Type Scripts. Look for 'Destination point given distance and bearing from start point'.

Here's an excerpt of the JavaScript from the site:

var lat2 = Math.asin( Math.sin(lat1)*Math.cos(d/R) + 
                      Math.cos(lat1)*Math.sin(d/R)*Math.cos(brng) );
var lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(lat1), 
                             Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2));

In the above code, d is the distance, brng is the bearing in degrees, and R is the Earth's radius.

Porting this to Java should be trivial.

Roman Nurik
This assumes that the Earth is flat, right?
Hamish Grubijan
I don't think so, but this method is far from precise. You can try this out on a 3D sphere here: http://earth-api-utility-library.googlecode.com/svn/trunk/extensions/examples/destination.html (requires the Google Earth Web Plugin)
Roman Nurik
This assumes a spherical Earth (that's why you need the `R`), but has an error proportional to the distance you travel. The absolute value of this error will be large over large distances, which is why it looks problematic for big values.
John Feminella