Using different map options

How do we display a map with different options such as a restricted number of map layers and map extent? 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;function init(){var options = {resolutions: [10, 5],maxExtent: new OpenSpace.MapBounds(400000, 100000, 450000, 150000) );osMap = new OpenSpace.Map('map', options);osMap.setCenter(new OpenSpace.MapPoint(438760,114760), 1);}</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:
var osMap;
Now we define our init function:
function init(){
Create an options object that holds a limited set of map resolutions and a maximum bounding extent to show the map. A new OS OpenSpace map object is created by passing in our 'map' HTML div element id and the map options.
var options = {resolutions: [10, 5],maxExtent: new OpenSpace.MapBounds(400000, 100000, 450000, 150000) );osMap = new OpenSpace.Map('map', options);
We now set the centre of the map and set the initial map zoom level to 1 (corresponding to the second defined layer in the resolutions array).
osMap osMap.setCenter(new OpenSpace.MapPoint(400000,100000), 1);
Finally, we close the function and script tag.
}</script>
The full example can be found below.
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Open Space Tutorial - Example 10</title><script type= "text/javascript"src="http://openspace.ordnancesurvey.co.uk/osmapapi/openspace.js?key=insert_your_api_key_here"></script></head><body onload="init()"><script type="text/javascript">var osMap;function init(){// Change default map[ options to just have two zoom levels// and a limited map extentvar options = {resolutions: [10, 5],maxExtent: new OpenSpace.MapBounds(400000, 100000, 450000, 150000) };osMap = new OpenSpace.Map('map', options);osMap.setCenter(new OpenSpace.MapPoint(438760, 114760), 1);}</script><h1>Using different map options</h1><div id="map" style="width: 500px; height: 300px; border: 1px solid black;"></div></body></html>
![OS OpenSpace [logo]](/images/openspace-logo.jpg)