Google Maps Distance (DistanceMatrix) API for UK in JSON

The postcode code has been updated to use Google’s distancematrix api, which provides a very different set of data from the old “as the bird flies” calculation (it calculates road distance, and provides transport options etc).

The following code is merely a demonstration, which can be seen here.

// Specify Postcodes to Geocode
$postcode1 = 'BH151DA';
$postcode2 = 'BH213AP';

// Set and retrieve the query URL
$request = "" . $postcode1 . "&destinations=" . $postcode2 . "&mode=driving&language=en-GB&sensor=false&units=imperial";
$distdata = file_get_contents($request);

// Put the data into an array for easy access
$distances = json_decode($distdata, true);

// Do some error checking, first for if the response is ok
$status = $distances["status"];
$row_status = $distances["rows"][0]["elements"][0]["status"];

if ($status == "OK" && $row_status == "OK") {

// Calculate the distance in miles
$distance = $distances["rows"][0]["elements"][0]["distance"]["value"];
$distance_miles = round($distance * 0.621371192/1000, 2);

echo 'The distance between '.$postcode1.' and '.$postcode2.' is '.($distance/1000).'Km ('.$distance_miles.' miles).';

} else {
    echo "Calculating the distance between your locations caused an error.";


Having better error checking would also be a good idea if you plan to use the above code. Using &unit=imperial is optional, as Google always returns metres – so the code runs a basic calculation on these to convert to miles.

Using Google Maps, Geocoding and PHP to find the distance between UK Postcodes

UPDATE: A new version which calculates distance based on roads is available.

If you’re looking to make any kind of radius checker, delivery calculator etc, you will need to have some method of calculating this distance. Unfortunately for us in the UK, Royal Mail keep a tight grip on postcode data.

As a result, the best low-budget way of finding postcodes is by using the Google Maps api – which in itself isn’t 100% accurate (but good enough).

So we can use the following code:

// Specify Postcodes to Geocode
$postcode1 = 'BH151DA';
$postcode2 = 'BH213AP';

// Geocode Postcodes & Get Co-ordinates 1st Postcode
$pc1 = ''.$postcode1.',+UK&output=csv&sensor=false';
$data1 = @file_get_contents($pc1);
$result1 = explode(",", $data1);
$custlat1 = $result1[2];
$custlong1 = $result1[3];

// Geocode Postcodes & Get Co-ordinates 2nd Postcode
$pc2 = ''.$postcode2.',+UK&output=csv&sensor=false';
$data2 = @file_get_contents($pc2);
$result2 = explode(",", $data2);
$custlat2 = $result2[2];
$custlong2 = $result2[3];

// Work out the distance!
$pi80 = M_PI / 180;
$custlat1 *= $pi80;
$custlong1 *= $pi80;
$custlat2 *= $pi80;
$custlong2 *= $pi80;

$r = 6372.797; // mean radius of Earth in km
$dlat = $custlat2 - $custlat1;
$dlng = $custlong2 - $custlong1;
$a = sin($dlat / 2) * sin($dlat / 2) + cos($custlat1) * cos($custlat2) * sin($dlng / 2) * sin($dlng / 2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));

// Distance in KM
$km = round($r * $c, 2);

// Distance in Miles
$miles = round($km * 0.621371192, 2);

echo 'The distance between '.$postcode1.' and '.$postcode2.' is '.$km.'Km ('.$miles.' miles).';


You could use $result1[0] and $result2[0] to check codes. If the value is anything other than 200 the postcode is invalid. Also note UK is also searched for to guarantee correct results!

The result is also rounded to make sure we only have 2 decimal places. Make sure your postcodes do not have any spaces in when they go to Google, if you’re collecting them from a form maybe use:

function nowhitespace($data) {
return preg_replace('/\s/', '', $data);
$postcode1 = nowhitespace($postcode1);

to remove all spaces before processing, and the following to check it’s ok after processing:

if (($result1[0] != 200) || ($result2[0] != 200)) {
echo "<p>Invalid Postcode(s) Entered. Please try again.</p>";
} else {

Good luck!

Selling more on – without the cost of PPC!

When you’re selling things online there always seems to be this “invisible barrier” between the sales volume you have and the sales volumes you would like to achieve. While eBay now offer all sorts of programs to “boost” sales there are often easier & cheaper things you can do to help your sales grow.

One of the most noticable changes was their introduction of the eBay PPC (pay per click) program, where you can advertise in places you normally wouldn’t be seen for a per-click rate (which of course is variable depending on the keywords, competition and placement for the advert). You could also call on traditional ad networks like Google Adwords to get your products out there, or even price comparison websites such as – again costing you per-click prices…with no guarantees of conversions.

I found the following article when digging through my bookmarks the other day, and while it is a couple of years old, many of the facts stand true today (SEO is more effective than PPC). What I want to take from it is that a good seo campaign will generally improve sales over time, and will keep driving sales even when you stop paying the bill. PPC is just a switch. As soon as you stop, sales slow.

Anyway – here’s a couple of ideas on how to boost your SEO rankings with your eBay shop:

  1. When creating listings, place the title of your item and other descriptive terms in H1 and H2 headers. These titles need to contain relevant keywords to your product, with high keyword density. When looking for keywords you can use the Google Keyword Analyser Tool to show search volumes for different keywords. The tool will also suggest other keywords to try. Try to think about terms people might search for most and put these in bold. Using the H1 and H2 headers essentially gives search engines a ‘snapshot’ of what that page might be about, and the bold text will be taken as important; giving it some extra weight in the search engines.
  2. Make sure you list products regularly to keep the search engines coming back for indexing. The more active your eBay store seems to the search engines, the higher (and more frequently) it will be listed.

  3. Make use of the free eBay tools such as Blogs, Reviews and Guides. You get free accounts with your seller account and you’ll see many of the larger sellers with tags such as “Top 25 Reviewer” etc. They have written lots of reviews or guides, which have then been indexed by the search engines. Every one of these reviews, posts or guides contains several links back to that seller’s shop and products.

  4. Use the RSS feeds included in eBay stores. You can automatically set Google to grab your products and essentially promote them for free in search results, comparison shopping and more. This used to be done through the google store connector, but now you have to setup the google base – ebay feed link yourself.

  5. Make use of the “Manage My Store” section in My eBay. Under Store Design, click “Search Engine Keywords.” You can use the Google Keyword Tool mentioned earlier to find high traffic keywords – but remember to only use keywords related to your store else you might be punished for keyword stuffing (using too many, or non-related keywords).

Thats all 🙂

Delete (Permanently) Campaigns from Adwords

EDIT: I’m aware that the adwords interface has now changed; thankfully it makes a little more sense now 🙂

So, am I missing something, or it a REQUIREMENT to have ALL of your old campaigns sitting in your account, forever?

Imagine how silly this would be, deleting some data, thinking it’s gone – but it’s still there…and sporting a big DELETED.

It’s confirmed that you can’t actually remove campaigns from Adwords, and one of our new staff was most confused.

Why keep the old campaigns? Well, because we all love Historical Data! Or that’s what Google says anyway.

There is an easy solution – you can set your account to ‘pretend’ the data was deleted. It’s in a little box to the top right of the campaign box…they explain it more here.

Thing is, you can easily remove the deleted campaigns from your sight – but you’ve still got their totals messing with your curent totals.

Google, I want a purge feature. Delete = Delete. Delete DOES NOT = Disable.

Funding Adwords with Adsense?

It’s been years since the first Adwords advertisers first commented on whether Google would ever release a funds transfer option for Adsense –> Adwords balances.

Just think how this would help cash flow in a company’s advertising budget – you could even call it renewable advertising.

Money In (from Adsense) = Money Out (from Adwords), and then of course you could throw an additional budget into the mix to further promote more visits, hence increasing the money made from adsense further – like a chain reaction.

This would greatly help us as a development and marketing company when working with our self managed customers. We set up all the marketing for them but use their accounts…so we’re constantly pushing for payments in and out of Google. Once funds have left the Adsense account it may be weeks (or on the odd occasion months) before the company accountants approve a push of funds back into the associated Adwords account.

There have been plenty of discussions over the years, outlining many of the problems of such a scheme, but we’ll keep hoping.

Another interesting problem is how Google deals with exchange rates when dealing with customer’s funds. I was reminded of this when reading a short article over at MEMWG on Adsense.

Whether this was down to how we configured Adsense or just how Adsense is, the whole system works in USD. I was reminded of Google’s binding to the American currency when the exchange rate for GBP-USD changed from the highs of less than a month ago, to new lows…where the Dollar is once again worth more against the Pound – meaning each USD in advertising revenue means more £GBP. Good stuff.

I then remembered that our Adwords accounts are all in GBP…which at the moment is favourable since we get more £ from our Adsense, and pay less for our Adwords than if our account was in Dollars.

That sounds about right. Point of the story there was that perhaps adoption of a single currency WOULD prove beneficial, else it could lead to people playing the currency markets through Google. Unlikely I know, but one has to take every opportunity one gets.

It would also remove most of the barriers to transferring balances between accounts.