vishal kokate
vishal kokate

Reputation: 134

How does Uber/Ola calculate distance traveled from start point to end-point of the trip?

Uber and Ola mobile app: To calculate distance between start point & end point of the trip we cannot use Google MAP API directly as it may not give the exact route traveled by the car. How do I drop points along the travel path so that I can use them to calculate the total distance traveled?

Upvotes: 1

Views: 2157

Answers (3)

vishal kokate
vishal kokate

Reputation: 134

From whatever research I have carried out in last few days. The following method works the best in order to find out total distance traveled in the trip:

  1. Keep track of (lat,lng) along the path traveled from Start point to End Point.
  2. At the end of the trip send a call to the server with all the co-ordinates captured.
  3. Add the distance calculated from combination of lat, lng.

In order to reduce the calls to distance calculation Google map API, it is better to send calls at the end of the trip at once.

Upvotes: 2

Mahammad Adil Azeem
Mahammad Adil Azeem

Reputation: 9392

var rad = function(x) {


return x * Math.PI / 180;
};

var getDistance = function(p1, p2) {
  var R = 6378137; // Earth’s mean radius in meter
  var dLat = rad(p2.lat() - p1.lat());
  var dLong = rad(p2.lng() - p1.lng());
  var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) *
    Math.sin(dLong / 2) * Math.sin(dLong / 2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  var d = R * c;
  return d; // returns the distance in meter
};

Source of above code

Upvotes: 0

Raphael Pedrini
Raphael Pedrini

Reputation: 11

My guess is that their app is running an onLocationChanged() on the background and capturing the car speed. If you have all the speed values during the trip, you can calculate the distance traveled.

Upvotes: 0

Related Questions