Usage:
./postcodetolonglat.sh 'SK99 9LZ'
Creates a little temp file called ‘postcode’. The file is put in /tmp and is of course unique for each execution of the program. These files can be cleared out quite easily as part of maintenance of the /tmp folder. (i.e. a weekly cron that executes
find /tmp -mtime +1 -user apache -exec rm {} \;
where the file will be at least a day old and is owned by apache which (in my case) is the apache user for the apache web server.
Script:
#!/bin/bash
. `/usr/bin/wget "http://maps.google.com/maps/geo?q=$1" -O "/tmp/$1"`
LAT=`cat "/tmp/$1" | awk -F "[" '{print $4}' | awk -F "," '{print $2}'`
LON=`cat "/tmp/$1" | awk -F "[" '{print $4}' | awk -F "," '{print $1}'`
echo "<script language=JavaScript>"
echo " var Lon=$LON;"
echo " var Lat=$LAT;"
echo " var orig='$1';"
echo "</script>"
Then in, say, google maps:
From your browser, call (for example for the Warminster postcode of the R.E.M.E - BA12 0BS)
http://server.com/map4.php?pc=BA12%200BS
The code for map4.php is below.
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<?php
$Postcode= $_GET['pc'];
echo passthru("./postcodetolonglat.sh '$Postcode'");
?>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAA2OgjrxQMCCnZOIKNKqdqqxQQ2LfZc7AlgWy0LicD3n_XCYU-EBSRAF96CM-HbX18JJscTSEShXpbfg"
type="text/javascript"></script>
<script type="text/javascript">
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map_canvas"));
var gll = new GLatLng(Lat,Lon);
map.setCenter(gll,17);
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
GEvent.addListener(map,"click", function(overlay,latlng) {
if (latlng) {
var myHtml = "The GLatLng value is: " + map.fromLatLngToDivPixel(latlng) + " at zoom level " + map.getZoom();
//map.openInfoWindow(latlng, myHtml);
alert(gll.toUrlValue(10));
}
});
}
}
</script>
</head>
<body onload="initialize()" onunload="GUnload()">
<div id="map_canvas" style="width: 100%; height: 100%"></div>
<div id="message"></div>
</body>
</html>