Reputation: 134
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
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:
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
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
};
Upvotes: 0
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