Displaying a polygon on a map

How do we add a filled polygon to the map? 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(){// Creates the map centered on OSHQosMap = new OpenSpace.Map('map');osMap.setCenter(new OpenSpace.MapPoint(438760, 114760), 6);var vectorLayer = new OpenLayers.Layer.Vector("Vector Layer");osMap.addLayer(vectorLayer);/** Define polygon style*/var style_green ={strokeColor: "#000000",strokeOpacity: 1,strokeWidth: 2,fillColor: "#00FF00",fillOpacity: 0.6};// Define polygon areavar p1 = new OpenLayers.Geometry.Point(439000, 114000);var p2 = new OpenLayers.Geometry.Point(440000, 115000);var p3 = new OpenLayers.Geometry.Point(437000, 116000);var p4 = new OpenLayers.Geometry.Point(436000, 115000);var p5 = new OpenLayers.Geometry.Point(436500, 113000);var points = [];points.push(p1);points.push(p2);points.push(p3);points.push(p4);points.push(p5);// create a polygon feature from a list of pointsvar linearRing = new OpenLayers.Geometry.LinearRing(points);var polygonFeature = new OpenLayers.Feature.Vector(linearRing, null, style_green);vectorLayer.addFeatures([polygonFeature]);}</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 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');
We now set the centre of the map above Ordnance Survey head office:
osMap.setCenter(new OpenSpace.MapPoint(438760, 114760), 8);
Add a vector layer to the map:
var vectorLayer = new OpenLayers.Layer.Vector("Vector Layer");osMap.addLayer(vectorLayer);
Define our particular line styling, including polygon fill colour, border width and opacity:
var style_green ={strokeColor: "#000000",strokeOpacity: 1,strokeWidth: 2,fillColor: "#00FF00",fillOpacity: 0.6};
This defines the polygon manually from a list of points:
// Define polygon areavar p1 = new OpenLayers.Geometry.Point(439000, 114000);var p2 = new OpenLayers.Geometry.Point(440000, 115000);var p3 = new OpenLayers.Geometry.Point(437000, 116000);var p4 = new OpenLayers.Geometry.Point(436000, 115000);var p5 = new OpenLayers.Geometry.Point(436500, 113000);var points = [];points.push(p1);points.push(p2);points.push(p3);points.push(p4);points.push(p5);
Create the polygon feature from the array of points:
var linearRing = new OpenLayers.Geometry.LinearRing(points);var polygonFeature = new OpenLayers.Feature.Vector(linearRing, null, style_green);
Finally, we add the polygon feature to the vector layer, and close both the function and the script tag.
vectorLayer.addFeatures([polygonFeature]);}</script>
The full example can be found below.
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Open Space Tutorial - Example 5</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;function init(){// Creates the map centered on OSHQosMap = new OpenSpace.Map('map');osMap.setCenter(new OpenSpace.MapPoint(438760, 114760), 6);var vectorLayer = new OpenLayers.Layer.Vector("Vector Layer");osMap.addLayer(vectorLayer);/** Define polygon styles*/var style_green ={strokeColor: "#000000",strokeOpacity: 1,strokeWidth: 2,fillColor: "#00FF00",fillOpacity: 0.6};// Define polygon areavar p1 = new OpenLayers.Geometry.Point(439000, 114000);var p2 = new OpenLayers.Geometry.Point(440000, 115000);var p3 = new OpenLayers.Geometry.Point(437000, 116000);var p4 = new OpenLayers.Geometry.Point(436000, 115000);var p5 = new OpenLayers.Geometry.Point(436500, 113000);var points = [];points.push(p1);points.push(p2);points.push(p3);points.push(p4);points.push(p5);// create a polygon feature from a list of pointsvar linearRing = new OpenLayers.Geometry.LinearRing(points);var polygonFeature = new OpenLayers.Feature.Vector(linearRing, null, style_green);vectorLayer.addFeatures([polygonFeature]);}</script></head><body onload="init();"><h1>Displaying a polygon on a map</h1><div id="map" style="width: 500px; height: 300px; border: 1px solid black;"></div></body></html>
![OS OpenSpace [logo]](/images/openspace-logo.jpg)