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.
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…
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, we will receive an affiliate commission. Regardless, we only recommend products or services we use personally and believe will add value to our readers.