2

I have two locations and I have the formula to get the distance between them but I want to calculate the travel time between them without using google maps or any API calling. Is there any formula to do that in flutter?

Useme Alehosaini
  • 2,998
  • 6
  • 18
  • 26

2 Answers2

0

I think the real travel time depends on a lot of factors, for example the road you take or the speed limit.

Whitout some complex elaboration, which are usually perfomed by a backend, I don't think it is possible to calculate it.

Anyway, since you have got the distance, you could simply estimate it by giving a supposed speed and then using basic physics formulas.

time = distance / speed.

L. Gangemi
  • 3,110
  • 1
  • 22
  • 47
  • thank you, i tried that but it sometimes it gives different values from google api, with big different values – asmaa mohamed magd Helali Oct 24 '20 at 13:14
  • As I said, for real travel time, you need to use complex algorithm (which takes care of a lot of different parameters) which are usually implemented in some complex backend which are accessible by API. This is just a physics formula, I think it's obvious that the results don't match. – L. Gangemi Oct 24 '20 at 13:59
  • If you think it helped, please consider upvoting. Thanks. – L. Gangemi Oct 25 '20 at 14:10
  • If this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – L. Gangemi Mar 18 '21 at 14:25
  • I just thought if there was a river between the two locations, the calculated driving time would be very different. – Daniyal Javani Sep 06 '22 at 15:57
  • If you know the location of some intermediate points, like a bridge, you could iterate the process and get a more precise result – L. Gangemi Sep 06 '22 at 16:07
0

You cannot find the travel distance between two locations without using the Google API. However you can get the air distance in meters using this formula in flutter.

double calculateDistance(
      double lat1, double lon1, double lat2, double lon2) {
    var p = 0.017453292519943295;
    var c = cos;
    var a = 0.5 -
        c((lat2 - lat1) * p) / 2 +
        c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p)) / 2;
    var temp = 12742 * asin(sqrt(a));
    return (temp * 1000);
  }
Muhammad Tameem Rafay
  • 3,602
  • 2
  • 21
  • 32
  • thank you so much, i did that but do you know is the result in km or meter ? , and it sometimes gives different values fro google api, do you know any other accurate formula ? – asmaa mohamed magd Helali Oct 24 '20 at 13:15
  • yes, its results in meters not in Km. Moreover, the result may vary a little bit because the latitude and longitude may vary for the same place. because the mobile caches different lat, long even for the same place. – Muhammad Tameem Rafay Oct 27 '20 at 08:16