# 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.

Asked by: Stuart202 | Posted: 25-01-2022

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.

Answered by: Roland776 | Posted: 26-02-2022

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.

Answered by: Ted769 | Posted: 26-02-2022

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.

Answered by: Rafael808 | Posted: 26-02-2022

Here is the reverse of it:

``````SphericalUtil.computeOffsetOrigin(loc1, dist, angle);
``````

It also has the distanceBetween function:

``````SphericalUtil.computeDistanceBetween(...);
``````

Lib:

SphericalUtil

Answered by: Aida338 | Posted: 26-02-2022

# Similar questions

geolocation - Android: Location.distanceBetween units and emulator not sending coordinates properly

Hy everyone, I have two questions 1) I have not been able to find out in which units Location.distanceBetween receivs the latitude and longitude. Is it degrees or microdegrees? Whats the unit for the distance returned? Sorry for this noob questions but I have not been able to find anything on the documentation. 2) In windows XP using Eclipse 3.3.2. Emulator does not send coordinates properly. Either it by h...

android - What scale is initial bearing in returned from Location.distanceBetween()

When calling Location.distanceBetween() and getting the initial and final bearing, what scale is the bearing in? I'm getting negative values, which doesn't make sense to me. Bearing goes from 0 to 360 degrees (relative or absolute, it doesn't matter). The only thing I could think of is N through E through S is 0 through 180 degrees, and N through W through S is 0 through -180 degrees. Can someone shed some light...

java - Android Location.distanceBetween and Location.distanceTo difference

I encounter some issues on trying to calculate distance between one location and a list of others (in order to get the closest one). I tried by using Location.distanceBetween(...) and location.distanceTo(...), the two method are working but both of them send me different data and not any of them seems right. My data came from a Class ConcessionDistance which is a basic extension of HashMap containing location data ...

android - Location.distanceBetween() gives continuously changing distance and bearing values

My code requires to find the distance between two Tablets and the direction of second Tablet with reference to the first one. Both the Tabs update their location every 10 seconds. But the distance as well as the initial and final bearing values vary as the locations update. Location.distanceBetween(destLatitude, destLongitude, sourceLatitude, sourceLongitude, result); float calc_distance = result...

android - Location.distanceBetweenPoints not returning meters?

I'm having a little trouble understanding this function: Location.distanceBetween(lat1, lon1, lat2, lon2, results); I have two points at my map, my current position and one other. Which should be approximatly 30 meters away from where I am. But still the results I get is: results = 6935649.5 results = 121.08577 results = 154.8537 Which doe...

google maps - android location.distanceBetween and Using previous location

I'm trying to calculate the length of a polyline by adding up the distances between points as I am moving. I'm wanting to use this method: distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results) I can place location.getLatitude() and location.getLongitude() in the endLat &amp; endLong. I'm placing all of this ...

android - NumberFormatException in Location.DistanceBetween result

i create location-based application. within the application there is a method to get user position. Some of users encountered that the app crash when trying to get user position. below is my get user position method. // Method to get user position public void getUserPosition(double latitude, double longitude){ // Check distance between user position and default position Location.distanceBetween(lati...

android - Location.distanceBetween doesn't get distance, range, meters, and not recognize latlong variables

I'm trying to get range from user if him let circle, but I can't get the range or the distance in screen, it always show 0.0, and don't show the toast. public class HomeActivity extends AppCompatActivity implements OnMapReadyCallback { /** * Request code for location permission request. * * @see #onRequestPermissionsResult(int, String[], int[]) ...

android - How to display result of Location.distanceBetween

Edit: It was suggested that I use this formula to calculate the distance between my current location and a marker that is placed, but I am getting the same result no matter where I place the marker, which is 1.004822115417686E7. I will show that method in my updated c...