Our taxi and courier client contacted us to develop an online pricing calculator that would reliably calculate the distance between two
locations (pickup and drop-off points). It would allow the user (customer) a choice of vehicle types ie. bike, van, car for the parcel(s) and provide
an estimated delivery cost based upon the locations and vehicle types chosen through their website.
Solution Understanding
Our initial thoughts were to use the “as the crow flies” solution, whereby postcodes and/ore addresses would be converted into coordinates
and then clever math applied to work out the distances. A percentage factor would then be introduced to the equation, which
would allow for road deviation that would naturally occur within geographic environments.
This solution would have been relatively easy to implement, however, it wouldn’t have been accurate enough for our client’s needs.
After all, our courier/taxi client would potentially lose revenue and possibly, over charge their customers. An accurately calculated
solution was required and having researched the web extensively our development team managed to source external mapping data.
Initial research
Following our research we discovered (Google’s distance matrix API)
. This proved to be the perfect solution as Google would return the distance between two free text addresses, whilst utilising and basing its calculations on the fastest route
between the two points.
Using the Google service weasn't stratightforward, it introduced a couple of issues of its own:
- Google doesn't provide a PHP version of their API, instead, it provides “services” for (Javascript),
(Python), (Java) and (Go)
- All data retrieved from Google's services must be displayed on a Google branded map.
CURL
The first issues was (thanks to Google's API) very simple to fix. Their API is beautifully simple and RESTful, which allowed for us to
easily write our own wrapper using CURL. An example of which can be found below:
<?php
// Create a new CURL instance<br>
$ch = curl_init();
// Api Url<br>
$url = "https://maps.googleapis.com/maps/api/distancematrix/";
// Array of options to be passed to API<br>
$options = array(
"origins" => "england",
"destinations" => "spain",
"units" => "imperial",
"language" => "en-GB",
"key" => API_KEY_HERE
);
$request = $uri . "?" . http_build_query( $options );
// set url <br>
url_setopt($ch, CURLOPT_URL, $request);
//return the transfer as a string <br>
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string <br>
$output = json_decode(curl_exec($ch), true);
// close curl resource to free up system resources <br>
curl_close($ch);
Let us help you – start your project
design and usability
The second issue was slightly more complicated as it meant the initial calculator design and usability of the application had to change. THis introduced another level of complexity.
Reworking the design wasn’t a problem for our website and user experience designers as a stylised Google map was simply added to the calculated cost panels, thus displaying pickup (point A) and drop-off (point B.)
There was a problem however, when a user/customer opted to display price comparison(s), more maps were created and thus slowing the page speed down to a crawl. Another Google API to the rescue!
Google provides a way of implementing what it calls “static maps” via the (Static maps API) these maps do not need
to be instantiated by Javascript and are instead just images produced via a specifically constructed URL. This allowed us to have as many images of maps on a page as we needed without
the worry of live maps slowing the page down.
The code that we used for our map looked something like this:
https://maps.googleapis.com/maps/api/staticmap?size=700x350&scale=1&maptype=roadmap\&markers=color:green|
label:A|eastgrinstead&markers=color:red|label:B|crawley
Thus generating a stylised map that looked similar to the below:
Finished solution
With the help of Google’s API’s we were able to create a seamless user experience and final end solution for our Taxi/Courier client’s online distance and price calculator.
If you are looking to integrate with Google’s mapping API or (for more information and full documentation of Google’s mapping API click here.)
Do you need a similar distance pricing calculator?
Contact our honest, transparent and intelligent team today to discuss your requirements.
Arrange your free consultation
Author
Wayne Frisby / Website developer & SEO consultant
Has this article helped you? If so I'd love to hear about it! Have I missed anything? Contact me on
Twitter @Wayne5and3