Blog

 

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.

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

// Set and retrieve the query URL
$request = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=" . $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.

read more
 
 

Raspberry Pi Shutdown Switch – Safely Turning off the Pi

We use a model B rev 2.0 Raspberry Pi as one of our Samba and DLNA media servers, and normally turn it off through a Webmin control panel to ensure nothing untoward happens with any of the attached hard drives. This solution is less than ideal however, as it requires a second powered-on device and a couple of clicks to get to the shutdown page in the control panel.

The Raspberry Pi forum has am excellent thread about a simple & safe shutdown button, from which I copied to produce my shutdown switch. It’s something you can put together in less than 20 minutes if you have the parts.

All you need is to install the RPi.GPIO Python class on the Pi (use SSH to grab it, “wget http://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.4.1a.tar.gz”; unzip it; and install it, “sudo python setup.py install”; then remove the files to tidy it all up), and you can start accessing the pins via Python scripts.

Just connect pin 1 to a momentary open switch, and the switch to a 1K and a 10K resistor. Then connect the 10K resistor to the ground on pin 9 and the 1K to pin 17. If you’re wondering where these are then this page has some nice illustrations; and more info on these pins can be found here. It’s worth covering the 5V line with some insulation just to minimise the risk of shorting out your Pi with it (see images below), as everything else on the GPIO header is 3.3V.

Breadboard setup, minus switch which would cross lanes 12 & 13.

Breadboard setup, minus switch which would cross lanes 12 & 13.

Raspberry Pi Shutdown Switch

In the case, soldered directly onto the pins with a momentary switch screwed into the case above the pi logo (hence minimising risk of contact)

All that’s left is to monitor pin 17 for activity, for which I used the code on the Raspberry Pi forum.

Placed in /home/pi/bin/button/shutdown.py

import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
while True:
if(GPIO.input(17)):
os.system("sudo shutdown -h now")
break
time.sleep(1)

Placed in /etc/rc.local on the line before “exit 0″

python /home/pi/bin/button/shutdown.py

Then the Pi will shutdown when the button is pressed for about 1 second – only the Power light will remain on (red) when it’s shut down. It’s worth noting that this won’t work if the Pi has crashed but that’s not an issue we often see.

 

SEATT Updated to 1.2.7

I’ve updated Simple Event Attendance (SEATT WordPress plugin) to version 1.2.7. This brings bug fixes, and updates in line with the release of WordPress 3.5.

  • Fixed problems with apostrophes being escaped with numerous backspaces in admin panel and in the comment box
  • Removed first+last name from page template as this is rarely used, with list users no longer in a table format, but now in an ordered list
  • Admins can now sign up registered users simply by supplying a username in the admin panel
  • Fixed problems with wp_prepare() causing errors in wordpress 3.5
  • Deleting the plugin now removes all database tables as well as files to provide clean removal. This means uninstalling will now remove all events

 

Live at http://wordpress.org/extend/plugins/simple-event-attendance/

 

SEATT Updated to 1.2.6

Simple Event Attendance has been updated to 1.2.6:

  1. Addition of extra table columns in admin view.
  2. Updated screenshots to reflect recent changes.
  3. Corrected use of date() function to current_time() to use timezone specified in WordPress rather than the server one.
  4. Added list of user emails signed up to an event.
  5. Some other small cosmetic changes, including register & login links on the event signup form.

The plugin is live at http://wordpress.org/extend/plugins/simple-event-attendance/

 

 
2

Facebook Like Buttons, &fb_xd_fragment=, Blank pages and SEO

This is a software development problem that’s been around for months now and yet still bugs thousands of users.

Any of your pages with facebook widgets on can result in URL’s like

www.example.com/page.php?fb_xd_fragment=

This has several implications. Firstly – it can cause blank pages through unwanted interactions with div areas on the page. We can solve this by adding the following fix just before the </body> tag. This came from http://forum.developers.facebook.net/viewtopic.php?id=60571&p=1 (temporary solutions)

<!-- Correct fb_xd_fragment Bug Start -->
<script>
document.getElementsByTagName('html')[0].style.display='block';
</script>
<!-- Correct fb_xd_fragment Bug End -->

The second, more long term issue is that this page will appear in search results alongside the normal page…resulting in duplicate content. Obviously you could just remove the like button but that’s not an ideal solution. So you can do a couple of things.

Head to webmastertools (https://www.google.com/webmasters/tools/home?hl=en) and add the fb_xd_fragment= as something that should be ignored on your site.

Filtering out fb_xd_fragment in Google Webmaster Tools

Filtering out fb_xd_fragment in Google Webmaster Tools

Another option is to use .htaccess and 301 redirects to clip out the &fb_xd_fragment=, which is a pain but very easily do-able and removes the requirement to put the display fix on every page. So try this (modified per your site) in your .htaccess.

RewriteCond %{QUERY_STRING} fb_xd_fragment=
RewriteRule ^(.*) http://www.example.com/$1? [R=301]

You could also ignore it and just hope facebook fixes it soon…yeahh bad choices right?