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:
- Sign up for Google Cloud Console account.
- 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.
- If you are a small business, you won’t need to worry about the billing for the Google Maps API
- Add a new project under the Google Maps Billing Account
- Enable Google Distance Matrix API for this new project
- Generate an API key from APIs & Services => Credentials => Create Credentials
- You have the API Key!
Understanding Basic Logic
- To calculate distance between 2 points, we need 2 GPS co-ordinates:
- 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.
- Customer’s Location (ask for the customer location inside a block on your bot)
- Set input type as Location in a block
- Set a dynamic variable for this block. we will use
locationfor this tutorial
- Store Location
- 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
- Go to the Bot Builder
- Edit the block where you want to do this (it should be a block following the block where you have asked for the location).
- Tick the HTTP-Request box on the top.
- Select GET and use this API URL
- Set units = metric or imperial based on your region
- Put the Store’s GPS coordinates in the origins parameter
- On successful API call, the Distance value will be accessible via this dynamic variable on botbaba –
- 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
- You can also use this Dynamic Variable in Conditional Delivery charges module to set delivery charges based on the distance.