-2

I'm working on the delivery web app and can't figure out how can I reduce the number of requests app sends to google distance matrix to calculate a distance between requested delivery and store address.

I have a page-catalogue that has around 1000 stores. Each time user enters delivery address I send 1000 requests to google maps API to check if user's delivery address is within each store delivery range. Hence google charges me for 1000 requests every time users enter new delivery address.

Any suggestions on how to optimise usage of Google API and show only those stores that deliver to the selected address, as the current way is the way too expensive. I'm wondering how large on-demand delivery services that have tens of thousands of stores deal with this?

Maark
  • 1
  • 2

1 Answers1

0

You could calculate the direct-line distance (using a formula) and only request stores whose direct line distance is less than the allowed range, since the travel distance can't be shorter than the direct line.

If you don't care about getting exactly the shortest travel, you can also sort the candidates, request them in order and stop as soon as you get an acceptable one. That will occasionally give a store that's physically closer but further away by road, which may or may not be acceptable.

In most programming languages, the direct-line distance will be available in a "geo" library or similar, under the name "great-circle distance". You can also search for it here on SO.

Jiří Baum
  • 6,697
  • 2
  • 17
  • 17