How can we help?
< All Topics
Print

How to set delivery charges based on distance?

Google Distance Matrix API Key is the main pre-requisite to achieve the distance calculation.

Here is how to obtain the Google Distance Matrix API Key:

  1. Sign up for Google Cloud Console account.
  2. Create a Maps Billing Account (its separate than a normal billing account). You will be asked to submit your credit/debit card details here but don’t worry, upto $200 worth of billing is free every month.
    1. If you are a small business, you won’t need to worry about the billing for the Google Maps API
  3. Add a new project under the Google Maps Billing Account
  4. Enable Google Distance Matrix API for this new project
  5. Generate an API key from APIs & Services => Credentials => Create Credentials
  6. You have the API Key!

Understanding Basic Logic

  1. To calculate distance between 2 points, we need 2 GPS co-ordinates:
    1. Store Location
      • Obtain this from Google Maps (Search for your store/business on G-Maps)
      • 21.1413367,72.7695557 is the store co-ordinates we will use in this example.
    2. Customer’s Location (ask for the customer location inside a block on your bot)
      1. Set input type as Location in a block
      2. Set a dynamic variable for this block. we will use location for this tutorial
  2. Now, this location variable (#location#) will contain the latitude and longitude (GPS Coordinates) of the customer, we pass it to Google Maps API to get the distance

Implementing on botbaba

  1. Go to the Bot Builder
  2. Edit the block where you want to do this (it should be a block following the block where you have asked for the location).
  3. Tick the HTTP-Request box on the top.
  4. Select GET and use this API URL
    1. https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=21.1413367,72.7695557&destinations=#location#&key=YOUR_GOOGLE_API_KEY
    2. Set units = metric or imperial based on your region
    3. Put the Store’s GPS coordinates in the origins parameter
  5. On successful API call, the Distance value will be accessible via this dynamic variable on botbaba – {{BLOCK_NAME_FROM_WHERE_YOU_MADE_THE_API_CALL.rows[0].elements[0].distance.value}}
  6. You can use this Dynamic Variable in conditional jump, to deny orders from distance more than ‘x’ miles/kms or only take orders from distance less than ‘x’ miles/kms
  7. You can also use this Dynamic Variable in Conditional Delivery charges module to set delivery charges based on the distance.

Was this article helpful?
2.8 out Of 5 Stars
5 Stars 50%
4 Stars 0%
3 Stars 0%
2 Stars 0%
1 Stars 50%
How can we improve this article?
Previous How to send notification on Chat Complete?
Next Regex Validation for custom length digits
Table of Contents