# Calculated Fields Form Blog

Tips and cases of use for a successful WordPress website with calculated forms.

Blog / Cases of use for a delivery project, transportation or any other project based on distance calculation

## Cases of use for a delivery project, transportation or any other project based on distance calculation

How to determine the price of a service that is based on the distance between addresses.

The "Calculated Fields Form" plugin is very versatile, it includes a large number of operations that allow to implement a wide range of projects.

In this article will be described a specific case of use: How to determine the price of a service that is based on the distance between addresses.

### Project description

Assuming it is a moving company, where the price is estimated based on the miles from the origin address to destination, multiplied by a factor determined by the company (for this article will be assumed the factor is \$1.05 per mile).

In a real moving company this form would include many other fields: contact information, the number of boxes, the floor, etc. But for simplicity we'll focus on those fields required to get the distance and price based on this distance.

### Form's fields

The basic fields in the form would be:

• A "Single Line Text" field for the origin address (fieldname1)
• A second "Single Line Text" field for destination address (fieldname2)
• The calculated field that determine the distance in miles, and multiply it by the factor mentioned above (\$1.05 per mile)

### The equation

This project requires to determine the distance between the addresses entered by the users. To facilitate this task the plugin includes the DISTANCE Operations Module distributed with the Developer and Platinum versions of the plugin, in particular the DISTANCE operation.

The protocol of the DISTANCE operation is:

DISTANCE(origin address, destination address, unit system, travel mode)

The possible unit system are: km (for kilometers), and mi (for miles) The possible travel modes are: DRIVING, BICYCLING, TRANSIT, WALKING, or STRAIGHT

With this information the equation associated to the calculated field would be:

``````PREC(DISTANCE(fieldname1, fieldname2, 'mi', 'DRIVING')*1.05, 2)
``````

The PREC(X, Y) rounds the number X with Y decimal places. In the previous equation, as it is a currency result, it is being rounded to 2 decimal places.

Pay attention: The DISTANCE operation is based on the Google Maps APIs, so, you need an API Key for using it, furthermore, in your Google Maps Project should be enabled the following APIs:

• Directions API,
• Maps JavaScript API,
• Distance Matrix API,
• Geocoding API,
• Places API (The places API is not needed until now, but would be required for the next section)

The Google Maps API Key is passed to the form through its shortcode, assuming the form id is 1, and the API Key: 1234567890, the shortcode would be:

`````` [CP_CALCULATED_FIELDS id="1" google_api_key="1234567890"]
``````

### Improving the users' experience

Entering the origin and destination addresses in a format understood by Google might be difficult in some cases, reduce the friction can make the difference between an awful or pleasurable user's experience.

To improve the user experience, it is possible to enable the Google Places add-on distributed with the Platinum version of the plugin. The Google Places add-on allows to transform some fields in the form into auto-complete address fields, recommending to the user a list of possible addresses based on the partial address typed by him.

### Integrating Google Places in the project

The first step would be enable the add-on from the settings page of the plugin.

• Go to the settings page of the plugin through the menu option: "Calculated Fields Form"
• Tick the box: "CFF - Google Places Integration"
• And press the "Activate/Deactivate Addons" button

The previous action will display a new section in the settings page of the plugin for entering the Google API Key, generated previously in the Google website (for using Google Places in the project, it is required to enable the Places API on Google)

Once enabled the add-on, and entered the API Key, is time to configure the form:

• Press the settings button corresponding to the form
• In the form's settings would be available a new section for integrate Google Place with the form's fields. In the screenshot below were entered the fieldname1 and fieldname2 fields, corresponding to the origin and destination addresses respectively. Furthermore, in this specific example, where the moving company operates in the United States of America, was entered entered the "US" code into the "Limit the search to the countries" attribute, for recommending only "United States" addresses.

### Public form screenshots

Populating the origin address

Populating the destination address

Equation's result