Need a break from coding? - Then try a boating holiday in Europe.

Google UK Maps API, plotting items onto UK Google Maps

Published : 23.May.2005

google uk maps

Introduction

There are many Google Maps mashups which demonstrate how sites are using Google Maps to enhance the user experience. First there was HousingMaps showing houses for sale and rent from craigs list. Then came Chicagocrime, plotting crime scenes for a particular day.

All these example are US based, and I wanted to explore the possibility of using the technique for UK maps, using the UK postal code as a method of plotting items of interest onto the UK version of Google Maps.

Converting from OS Grid Co-ordinates to
WGS84 Degrees Minutes and Seconds

Most postcode data sets and services, including my own find nearest application, use easting and northings measured in meters based on the Ordnance Survey Grid. The first step to being able to plot UK postcodes onto the Google mapbase was to find a way of converting and transforming the co-ordinates to the nationally recognised WGS84 Degrees Minutes and Seconds format.

For example (PR2 9HT)
has OSGB co-ordinates of :-

353600,432900

Using PROJ 4.4.6

After searching the Internet I found that Proj.4 could quite easily convert the OSGB co-ordinates into a WGS84 Degrees Minutes and Seconds value.

Example usage:-

C:\> proj.exe -I +init=world:bng +ellps=WGS84
353600 432900                        (input)
2d42'15.556"W   53d47'22.952"N       (output)
<cntl-Z>
C:\>

Converting WGS84 Degrees Minutes and Seconds into Decimal Degrees

Google Maps plots items onto the map using longitude and latitude values represented as decimal degrees. So now that you have your postcode PR2 9HT transferred to DMS 2d42'15.556"W 53d47'22.952"N you need to convert each of these into decimal degrees.

The formula for this is :-

Decimal Degrees = Degrees + (Minutes/60) + (Seconds/3600)

Therefore :-

longitude =  2 + (42/60) + (15.556/3600)
latitude  = 53 + (47/60) + (22.952/3600)

results in :-
longitude = 53.78970889
latitude  = 2.704321111

Adjusting for zero degree latitude

As the UK is north of the equator, the longitude will be (N) north, so this value will not need adjusting. However as the Greenwich Meridian runs through the UK, any latitude value that has a (W) west setting will need its decimal degree value turning into a negative, as its to the left of the Greenwich Meridian. Any value with an (E) east setting can remain as a positive value.

Therefore our sample becomes :-

longitude = 53.78970889
latitude  = -2.704321111

The Results

Once you have the longitude and latitude values, you can use the Google Maps API to produce very detailed interactive map applications. Here is one I produced for a Nescafe promotion, which shows all the shops in the UK which took part in a promotion.

Connecting To The Google Maps Geocode Service
Using PHP To Return Latitude And Longitude Values In Real Time.

At the beginning of August I noticed that Google offers a geocode application via HTTP Requests, which allows you to obtain the Latitude and Longitude values for any UK postcode and returns the results as a CSV file, which is very easy to parse. Below is some sample PHP code which demonstrates how to use the geocode service with your Google Maps API key.

$google_api_key = 'enter_your_api_key_here';
$postcode       = 'PR6 0LA';

list($status, $accuracy, $latitude, $longitude) =
    split(',', geocode_postcode ($postcode, $google_api_key));

/* if the return status is 200 then
   the lookup was successful */

if($status == '200') {
    echo '<pre>';
    echo 'Postcode = ', $postcode,  "\n";
    echo 'Latitude = ', $latitude,  "\n";
    echo 'Latitude = ', $longitude, "\n";
    echo '</pre>';
}

Below is the PHP code which makes up the function google_geocode_postcode which uses fsockopen to connect to the geocoding service and parse out the results. You will need to include this function within your PHP scripts.

function geocode_postcode ($postcode, $google_api_key) {

    $response = '';
    $values   = '';

    /* remove spaces from postcode */
    $postcode = trim($postcode);
    $postcode = str_replace(' ', '', $postcode);

    /* connect to the google geocode service */
    $fp = fsockopen("maps.google.com", 80, $errno, $errstr, 15);

    if (!$fp) {
        $response = ' error: ' . $errno . ' ' . $errstr;
        return $response;
    } else {
        $out  = "GET /maps/geo?q=" . $postcode . 
            "&output=csv&key=" . $google_api_key .
			" HTTP/1.1\r\n";

		$out .= "Connection: Close\r\n\r\n";

        fwrite($fp, $out);
        while (!feof($fp)) {
            $response .= fgets($fp, 4096);
        }
        fclose($fp);
    }
    
    /* parse out the values from the response */
    $values = $response;

    $int_pos1 = 0;
    $int_pos2 = 0;
    $int_pos1 = strpos($values, "200,");
    $int_pos2 = strpos($values, "\n", $int_pos1) -1;
    
    $values = substr($values, $int_pos1, $int_pos2 - $int_pos1);

    return $values;
}

Related Article

See also my article on Creating a Driving Directions system for your web site visitors.

Additional Reading

What Next?

Bookmark this article at :-

If the information you have found here helps to strengthen your business, or has saved you lot's of time, and you'd like to show your appreciation, consider making a small donation.