Reverse Location.distanceBetween()
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.
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.
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.
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.
Here is the reverse of it:
SphericalUtil.computeOffsetOrigin(loc1, dist, angle);
It also has the distanceBetween function:
SphericalUtil.computeDistanceBetween(...);
Lib:
SphericalUtil
精彩评论