PHPBuilder - UK Postcode Map



RSS Twitter
Snippets Graphics

UK Postcode Map

by: Will Platts
|
December 13, 2003

Version: 1.0

Type: Full Script

Category: Graphics

License: GNU General Public License

Description: This takes a postcode as a get variable, then converts it to a longitude and latitude from a database. It then plots this position on a map of the UK. *NOTE: You need to have GD installed*



<?php
$link = mysql_connect("localhost", "root", "");
mysql_select_db("places");

function getpost($postcode)
{
    $char = 1;
    do {
        $part = substr($postcode, 0, $char);
        $result = mysql_query("SELECT * FROM codes WHERE post LIKE '$part%'");
        $rows = mysql_num_rows($result);
        ++$char;
        }
    while ($rows > 1);

    $results = mysql_fetch_array($result, MYSQL_ASSOC);
    return $results['post'];
}

$post = getpost($_GET['postcode']);

$postcode = $post;

$sql = "SELECT * FROM codes WHERE post='$postcode'";
$results = mysql_query($sql);
$row = mysql_fetch_array($results);


$latitude = $row[5];
$longitude = $row[6];
settype($latitude, "float");
settype($longitude, "float");

$longwidth = (10.40480+1.76791);   //   12.17271
$latheight = (58.67925-49.97763);//8.70162
$canvaswidth = 381;
$canvasheight = 440;


$amountleft = (((10.40480+$longitude)/$longwidth)*$canvaswidth);
$amountdown = ($canvasheight-(((($latitude-49.97763)/$latheight))*$canvasheight));

$amountleft = round($amountleft);
$amountdown = round($amountdown);

$image = imagecreatefrompng("croppedmap.png");
$red = imagecolorallocate($image, 255,0,0);
imagefilledrectangle($image,$amountleft-2,$amountdown-2,$amountleft+2,$amountdown+2,$red);
header ("Content-type: image/png");
imagepng($image);
imagedestroy($image);

?>

**GET DATABASE OF UK POSTCODES HERE: http://www.jibble.org/ukpostcodes/ **

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Will Platts

Comment:



Comment:

(Maximum characters: 1200). You have characters left.