- Home
-
Public Sector
Public Sector
Quick links to popular content
- Business
-
Leisure
Leisure
OS getamap – print your route
Have your map custom made
Visit our shop
Read our magazine
-
Education & Research
Education & Research
Schools
Further & Higher Education
Research
General interest
-
About us
About us
Quick links
- Support
Adding a single boundary layer with default styling
How do we add a single boundary layer with default styling? We are going to build on displaying a marker tutorial. In this tutorial we created an HTML document with all the appropriate tags and included a DIV tag which gave instructions on how to display the map. The heavy lifting is all done within the javascript section (between the script…/script tags). The instructions in this document will deal with this core of the code. Essentially we will be replacing the content of displaying a marker tutorial script tags, with the following:
<script type="text/javascript">
var osMap, boundaryLayer;
function init()
{
osMap = new OpenSpace.Map('map');
var options = {strategies: [new OpenSpace.Strategy.BBOX()],
area_code: ["CTY" ]
};
boundaryLayer = new OpenSpace.Layer.Boundary("Boundaries", options);
osMap.addLayer(boundaryLayer);
osMap.setCenter(new OpenSpace.MapPoint(400000, 200000), 2);
}
</script>
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 boundaryLayer object variable, boundaryLayer:
var osMap, boundaryLayer;
Now we define our init function:
function init()
{
Create a new OS OpenSpace map object and pass it our 'map' element id. This is the HTML div that will hold the map.
osMap = new OpenSpace.Map('map');
Then we create an options variable that contains strategies that will be used to refine the boundaries that are returned. The bounding box strategy(OpenSpace.Strategy.BBOX()) is compulsory. The area_code is one such strategy of which there are others (see documentation). In this case we just wish to return only county boundaries (area_code = "CTY"):
var options = {strategies: [new OpenSpace.Strategy.BBOX()],
area_code: ["CTY" ]
};
Then we populate our boundaryLayer object using the syntax required to return boundaries and with the options that we have just defined:
boundaryLayer = new OpenSpace.Layer.Boundary("Boundaries", options);
Then we add the boundary layer to the map:
osMap.addLayer(boundaryLayer);
However, we then need to move the map around so that the boundaries are actually returned. This can be the same as the current location:
osMap.setCenter(new OpenSpace.MapPoint(400000, 200000), 2);
}
Finally close the script.
</script>
The full code can be found below:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Open Space Tutorial 6</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, boundaryLayer;
function init()
{
osMap = new OpenSpace.Map('map');
var options = {strategies: [new OpenSpace.Strategy.BBOX()],
area_code: ["CTY" ]
};
boundaryLayer = new OpenSpace.Layer.Boundary("Boundaries", options);
osMap.addLayer(boundaryLayer);
osMap.setCenter(new OpenSpace.MapPoint(400000, 200000), 2);
}
</script>
</head>
<body onload="init()">
<H1>Add a boundary</H1>
<div id="map" style="width: 500px; height: 300px; border: 1px solid
black;"></div>
</body>
</html>