My Story When Working With Google Distance Matrix API
A few months ago when I’m working on my freelancing project. This project is like to build an application like Careem or Uber. You see an application like Careem or Uber required a lot of working with Google Maps, Places and Distance Matrix API and many others. I’ve tackled a lot of difficulties when working on this application but there’s one problem where I stuck for days.
Problem
The Uber and Careem have their driver app at which passenger request for the ride. After accepting the passenger request driver needs to reach passenger location and then the driver starts the ride. When the ride starts the driver application start tracking the distance from origin to the passenger destination. At first, I think that’s easy store the location when a driver starts the ride and execute the distance matrix request with the destination location. That’s it!
Distance Tracking Problem
One day, when I’m testing the app I start the ride from my house go to the market came back then complete the ride. But when I see the total traveling distance is just two meters I shocked. I said, to myself, the market is two kilometers away from my home and that’s the one-way distance. So the total distance needs to be 4 kilometers. After an hour of thinking I came up with the problem that my start ride co-ordinates and end ride co-ordinates are the same.
Distance Tracking Solution
Another solution which I come up with after searching on StackOverflow and talking with other developers on Reddit is to execute the Distance Matrix API request whenever the driver current location changed. This approach is better than the previous one and most of all calculate the correct tracking distance. I need to do two extra things with this approach.
- I need to request the Google Distance Matrix API every time when the driver current location changed.
- Need to store the result of Distance Matrix API, store the destination location and again when the current location change executes the request with the previously stored location and so on…
Distance Matrix Billing Problem
Everything seems to be working fine unless I see the graph of Distance Matrix API graph in google console. After every three seconds, one request is executed for distance calculation.
Distance Matrix Billing Solution
Another day at work and need to find out another solution for distance tracking. At the end of the day I finally able to find a solution which generates less billing and better in performance from the previous one. Now instead of executing distance request whenever the driver current location changed, I started a background service and also increase the interval for Distance Matrix network request. The new interval is ten seconds, so it means the API billing is three times less.
I know there are many other solutions which may be better than my final explanation. Any suggestion, comments, and corrections are always welcome.
In the last, I just want to say that if you’re an Android Developer then do check out the next article. In the next article, I’m gonna to make a location distance tracking application.
Thank you for being here and keep reading…
This is my today question. I cannot find any affordable solution. Think an app with more than 10.000 how the bill will be if the interval is 10 sec and a user is using the app for one hour.. The bill will be astonishing..
https://stackoverflow.com/questions/57537037/how-calculate-google-distance-matrix-elements-per-request-for-real-time-tracking?noredirect=1#comment101538871_57537037
Yes you’re right “The bill will be astonishing”. There are couple of other ways to calculate the travelling distance from point A to Z without using the Google Distance Matrix API.
1. Location.distanceTo
2. Distance calculation method
The both of above methods calculate the straight-line distance which is not good for street which tends to be at right angles. Also, these methods are not good if I need to calculate the travelling distance after some x seconds. For better clarification why I did not preffer to use these methods see my comment on reddit post.
Now you need to think what x seconds will be suitable for your scenario. In my case, I’m happy with 10 seconds.
Awesome post.