0

How I can find the distance in kilometre between two location with their longitude and latitude using java and android? I retrieve the longitude and latitude from the Firebase database which I stored them in it, but the problem I need a way to find the actual distance not the straight line distance.

r_hm
  • 3
  • 5
  • the actual distance as in accounting for the curvature of the earth? – silversunhunter Mar 23 '21 at 22:18
  • yes, as when it is displayed in the google map when put two location and give us the time to reach it and the distance – r_hm Mar 23 '21 at 22:20
  • I need to calculate this distance from the longitude and latitude that I have – r_hm Mar 23 '21 at 22:20
  • If you want to travel time and distance by road I would suggest using a map api like google, apple or other.... – silversunhunter Mar 23 '21 at 22:22
  • 1
    Just divided this by 1000 or adjust algorithm (this can also take into account altitude differences : https://stackoverflow.com/a/16794680/4252352 - remember this is "as the crow flies". – Mark Mar 23 '21 at 22:23
  • Can use it with the longitude and latitude that is already taken ? – r_hm Mar 23 '21 at 22:24
  • @silversunhunter this will give the straight line distance not the actual distance as it displayed in map – r_hm Mar 23 '21 at 22:30
  • what is the actual distance? [Great-Circle](https://en.wikipedia.org/wiki/Great-circle_distance)? –  Mar 23 '21 at 22:44
  • 1
    For short distances, where straight-line calculations are fine, use `Location.distanceBetween()`: https://developer.android.com/reference/android/location/Location#distanceBetween(double,%20double,%20double,%20double,%20float[]) – CommonsWare Mar 23 '21 at 23:01
  • @Great-Circle I meant of actual distance is the distance that displayed in google map when I put my location and my destination which consider the heights and deviations of roads – r_hm Mar 23 '21 at 23:13
  • 1
    distances between to points using geographical transport infrastructure routes is a lot more complicated as it involves weighed graphs, adjacency lists, Dijkstra's algorithm etc. Having the start and end points is the entry point into all of this. I would look into what information is available to you, other than start and end points and then invest some time learning weighed graphs. – Mark Mar 23 '21 at 23:41

0 Answers0