Postcode search

How do we find the location of a postcode or a postcode sector and centre the map on it? The following example allows entry of a correctly spelt postcode e.g. SO16 4GU or SO164GU or a postcode sector e.g. SO16.
First, we need to tell the browser to pull in the OS OpenSpace JavaScript API:
<script type= "text/javascript"src="http://openspace.ordnancesurvey.co.uk/osmapapi/openspace.js?key=insert_your_api_key_here"></script>
Next, in the body, we need to tell JavaScript to run our initialisation function on loading the page:
<body onload="init();">
Next we need to add a map div element; this is the HTML element that will contain the map:
<div id="map" style="width: 500px; height: 300px; border: 1px solid black;"></div>
Now we move on to the core of the code, actually instantiating and adding the map:
<script type="text/javascript">var osMap, postcodeService;function init(){osMap = new OpenSpace.Map('map');osMap.setCenter(new OpenSpace.MapPoint(438760,114760), 9);postcodeService = new OpenSpace.Postcode();}function searchPostcode(){var query = document.getElementById("postcode");postcodeService.getLonLat(query.value, onResult);return false;}function onResult(mapPoint){osMap.setCenter(mapPoint, 7);}
Let's break this up:
We start a script tag to contain our code:
<script type="text/javascript">
Then we create a map object variable, osMap and a variable for the postcode search facility, postcodeService:
var osMap, postcodeService;Now we define our init function:
function init() {Then create a new OS OpenSpace map object and pass it our 'map' element id. This is the HTML div that will hold the map:
var osMap, postcodeService;
Next we centre the map on our choice of location and zoom level:
osMap.setCenter(new OpenSpace.MapPoint(438760,114760), 9);
Then we start up the postcode service which is where we search Code-Point® for a match:
postcodeService = new OpenSpace.Postcode();
This function is called when the user clicks the 'go' button.
function searchPostcode(){
The input value to the form is read into a variable:
var query = document.getElementById("postcode");
The postcode service is searched to see if there is a match and if there is a MapPoint is sent to a function (onResult in this case):
postcodeService.getLonLat(query.value, onResult);return false;}
Finally, if the postcode or sector was found the map is centred at the centroid of the Code-Point® polygon and at a zoom level of our choosing:
function onResult(mapPoint){osMap.setCenter(mapPoint, 7);}
The full code can be found below:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Open Space Tutorial - Example 11</title>
<script type="text/javascript" src="http://openspace.ordnancesurvey.co.uk/osmapapi/openspace.js?key= insert_your_api_key_here "></script>
<script type="text/javascript">
var osMap, postcodeService;
function init()
{
osMap = new OpenSpace.Map('map');
osMap.setCenter(new OpenSpace.MapPoint(438760,114760), 9);
postcodeService = new OpenSpace.Postcode();
}
function searchPostcode()
{
var query = document.getElementById("postcode");
postcodeService.getLonLat(query.value, onResult);
return false;
}
function onResult(mapPoint)
{
osMap.setCenter(mapPoint, 7);
}
</script>
</head>
<body onload="init()">
<div id="map" style="width: 500px; height: 300px; border: 1px solid black;"></div>
<form onsubmit="return searchPostcode();">
<span><b>Enter postcode or postcode sector:</b></span>
<input type="text" value="" id="postcode" size="10" maxlength="10"/>
<input type="button" value="Go" onclick="searchPostcode();"/>
</form>
</body>
</html>
![OS OpenSpace [logo]](/images/openspace-logo.jpg)