Need some help?
Frequently Asked Questions
A list of frequently asked questions, covering general issues including getting started and terms of use, is available from our FAQ section. Advanced and Technical FAQs are on this page.
OS OpenSpace Forum
Our secure forum is a place where developers can talk about OS OpenSpace issues. You will find many questions and solutions offered up this developer community.
OS OpenSpace demo
This demo features some of the code examples listed below.
- Basic Map
- Add a marker
- Create a polyline
- Create a polygon
- Do a gazetteer search
- Do a postcode search
- Display cursor location
- Display an administrative boundary
- Change the colour of the administrative boundary
- Show ward boundaries
End User Licence Agreement for OS Openspace Example Application.
Basic Map:
To help you get going, this code adds a basic map. It's possible to change the map size, centre coordinate position and zoom level in the code. The extent that you can pan around the map has been restricted in this example.
Code snippet:
//first we restrict the extent that you can pan around the mapvar options = {restrictedExtent: new OpenSpace.MapBounds(390500, 362700, 472600, 413500)};//the osMap variable refers to a div element whose size we have set in the html bodyosMap = new OpenSpace.Map('map', options);//set centre of map and zoom levelosMap.setCenter(new OpenSpace.MapPoint(430218.5, 383541.8), 5);
Add a marker:
You can use a marker to pinpoint a location on the map. The marker can be opened to show images or text. Many markers can be added to a map. Click on the marker in this example.
Code snippet:
//specify the location where the marker will be displayed on the mappos = new OpenSpace.MapPoint(432833.86, 384671.15);//specify the html content for the popup and its sizevar content = 'Info window contents can be in <strong>HTML</strong><br />And cancontain links:<br /> <a href="index.html">Back to Index</a><br />And images:<br><img src=\"openspace-logo.jpg\" width=\"120\" height=\"32\" />';var popUpSize = new OpenLayers.Size(300, 200)//add the marker to the map - null means no custom iconvar marker = osMap.createMarker(pos, null, content, popUpSize );
Create a polyline:
You can show routes on the map by inserting waypoint coordinates within the code. A waypoint is usually where the route changes direction.
Code snippet:
//initialise our vector layer variablevectorLayer = new OpenLayers.Layer.Vector("Vector Layer");//define the line stylevar style_green = {strokeColor: "#00FF00", strokeOpacity: 1, strokeWidth: 6};//push points into array variable(named 'points')points.push(new OpenLayers.Geometry.Point(430170.54, 387588.11));points.push(new OpenLayers.Geometry.Point(427951.11, 386890.58));points.push(new OpenLayers.Geometry.Point(425034.14, 387841.77));points.push(new OpenLayers.Geometry.Point(423512.24, 388285.65));points.push(new OpenLayers.Geometry.Point(420278.22, 386700.35));points.push(new OpenLayers.Geometry.Point(419897.74, 384734.56));points.push(new OpenLayers.Geometry.Point(420722.11, 382515.13));points.push(new OpenLayers.Geometry.Point(422941.54, 381120.05));points.push(new OpenLayers.Geometry.Point(424146.37, 377632.38));points.push(new OpenLayers.Geometry.Point(426746.28, 380485.94));points.push(new OpenLayers.Geometry.Point(428268.18, 380676.17));points.push(new OpenLayers.Geometry.Point(429473.01, 382007.83));points.push(new OpenLayers.Geometry.Point(430868.07, 382261.48));points.push(new OpenLayers.Geometry.Point(432009.5, 383656.55));points.push(new OpenLayers.Geometry.Point(432833.86, 384671.15));//create a polyline feature from the array of pointsvar lineString = new OpenLayers.Geometry.LineString(points);var lineFeature = new OpenLayers.Feature.Vector(lineString, null, style_green);vectorLayer.addFeatures([lineFeature]);//add it to the maposMap.addLayer(vectorLayer);
Create a polygon:
You can create your own polygon to show an area of interest. This is similar to creating a route except that, in addition, the polygon is closed and can be coloured in.
Code snippet:
vectorLayer = new OpenLayers.Layer.Vector("Vector Layer");//define our particular line styling, including width and colour of the line:var style_green ={strokeColor: "#000000",strokeOpacity: 1,strokeWidth: 2,fillColor: "#00FF00",fillOpacity: 0.6};//push points into an array variable(named 'points')points.push(new OpenLayers.Geometry.Point(430170.54, 387588.11));points.push(new OpenLayers.Geometry.Point(427951.11, 386890.58));points.push(new OpenLayers.Geometry.Point(425034.14, 387841.77));points.push(new OpenLayers.Geometry.Point(423512.24, 388285.65));points.push(new OpenLayers.Geometry.Point(420278.22, 386700.35));points.push(new OpenLayers.Geometry.Point(419897.74, 384734.56));points.push(new OpenLayers.Geometry.Point(420722.11, 382515.13));points.push(new OpenLayers.Geometry.Point(422941.54, 381120.05));points.push(new OpenLayers.Geometry.Point(424146.37, 377632.38));points.push(new OpenLayers.Geometry.Point(426746.28, 380485.94));points.push(new OpenLayers.Geometry.Point(428268.18, 380676.17));points.push(new OpenLayers.Geometry.Point(429473.01, 382007.83));points.push(new OpenLayers.Geometry.Point(430868.07, 382261.48));points.push(new OpenLayers.Geometry.Point(432009.5, 383656.55));points.push(new OpenLayers.Geometry.Point(432833.86, 384671.15));//create a polygon feature from the array of points and using the style from abovevar linearRing = new OpenLayers.Geometry.LinearRing(points);var polygonFeature = new OpenLayers.Feature.Vector(linearRing, null, style_green);vectorLayer.addFeatures([polygonFeature]);//add it to the maposMap.addLayer(vectorLayer);
Do a basic gazetteer search:
The gazetteer has over 250,000 place names and you can add a search box to find towns, villages, and all other names found on the 1:50,000 raster map.
Code snippet:
//set-up the gazetteer servicevar osGaz = new OpenSpace.Gazetteer;/*this can be entered via user input but as an example we will pre-define the name that we are searching for */var placeStr = "Tideswell"//pass any matches of the name to a function(gazOptions)var gazArray = osGaz.getLocations(placeStr, gazOptions)}function gazOptions(searchVal){//in this case there is only one match so we zoom to it, if there are more we can offer them to the user to selectosMap.setCenter(searchVal[0].location, 7);}
Do a postcode search:
This allows you to search by full postcode or postcode sector.
Code snippet:
//initialise the postcode service into a variablevar postcodeService = new OpenSpace.Postcode();//pass our input, in this case 'hard-wired', to a functionpostcodeService.getLonLat("S9 5NF", onResult);}function onResult(postcodeCentre){/*if found, a MapPoint is passed into this function and the map is centered on it atzoom level 10 */osMap.setCenter(postcodeCentre, 10);}
Display cursor location:
To find the coordinates of a place, move the cursor around the map.
Code snippet:
/*create a screen overlay called "coords" and position it ten pixels from the right and top of the map window. Add this as a new layer to the map. This layer will then remain at the same position relative to the map window as the map is scrolled underneath.*/screenOverlay = new OpenSpace.Layer.ScreenOverlay("coords");screenOverlay.setPosition(new OpenLayers.Pixel(240, 10));osMap.addLayer(screenOverlay);//we create an object to handle the coordinate transformation.gridProjection = new OpenSpace.GridProjection();//register what to do when the mouse moves over the maposMap.events.register("mousemove", osMap, createCursorPos);//function that is called as a resultfunction createCursorPos(e){/*the mouse position (e) is converted into a map position via the OpenLayers function getLonLatFromViewportPx(). This returns a point in the coordinate system of the map base layer, in our case this is British National Grid. We can then transform that point back into longitude/latitude by calling our previously defined convertor function. Finally the details are displayed in the screen overlayby setting the HTML contents. */pt = osMap.getLonLatFromViewPortPx(e.xy);lonlat = gridProjection.getLonLatFromMapPoint(pt);screenOverlay.setHTML("<DIV style=\"width: 150px; height=75px; color: white; background-color: #222; font-size: 20px\">" +"E: " + pt.lon + "<BR>" + "N: " + pt.lat + "<BR>" + "LON: " + (lonlat.lon).toFixed(4) + "<BR>" + " LAT: " + (lonlat.lat).toFixed(4) + " </DIV>");osMap.setCenter(osMap.getCenter);}
Display an administrative boundary:
Code snippet:
/*here we set-up the our variable called 'boundaryLayer' with the strategies that we require.In this case, it is its ID and type i.e. Westminster constituency */boundaryLayer = new OpenSpace.Layer.Boundary("Boundaries", {strategies: [new OpenSpace.Strategy.BBOX()],admin_unit_ids: ["24703"],area_code: ["WMC"]});//then we add the bounadry to the maposMap.addLayer(boundaryLayer);//this effectively refreshes the map, so that the boundary is visibleosMap.setCenter(osMap.getCenter());
Change the colour of the administrative boundary:
Data derived from the Boundary-Line&trade product can be styled to create for example thematic maps:
Code snippet: //set-up default symboliservar symbolizer = OpenLayers.Util.applyDefaults({ }, OpenLayers.Feature.Vector.style["default"]);//and a new style mapvar styleMap = new OpenLayers.StyleMap(symbolizer);//this is our custom stylinglookup = {"WMC":{fillColor: "red",fillOpacity: 0.6,strokeColor: "white",strokeWidth: 3,strokeOpacity: 0.7}};//add rules to the style mapstyleMap.addUniqueValueRules("default", "AREA_CODE", lookup);//create a new boundary using the above styling and a specific boundaryboundaryLayer = new OpenSpace.Layer.Boundary("Boundaries", {strategies: [new OpenSpace.Strategy.BBOX()],admin_unit_ids: ["24703"],area_code: ["WMC"],styleMap: styleMap });//add boundary to the map and refresh to make visibleosMap.addLayer(boundaryLayer);boundaryLayer.refresh();
Show ward boundaries:
Using area codes it is possible to display the boundaries that you require. In this case it is district and metropolitan wards. The style map is optional. Notice how the resolution of the boundaries changes, as you zoom in and out.
Code snippet:
//set -up style for the boundaries that we requirevar symbolizer = OpenLayers.Util.applyDefaults({ }, OpenLayers.Feature.Vector.style["default"]);var styleMap = new OpenLayers.StyleMap(symbolizer);lookup = {"MTW": {fillColor: "green",fillOpacity: 0.3,strokeColor: "white",strokeWidth: 3,strokeOpacity: 0.7},"DIW": {fillColor: "blue",fillOpacity: 0.3,strokeColor: "white",strokeWidth: 3,strokeOpacity: 0.7}};//add rules to our syle mapstyleMap.addUniqueValueRules("default", "AREA_CODE", lookup);/*request boundaries and add to map - we just want the type of wards that arefound in our area of interest */boundaryLayer = new OpenSpace.Layer.Boundary("Boundaries", {strategies: [new OpenSpace.Strategy.BBOX()],area_code: ["MTW", "DIW"],styleMap: styleMap});osMap.addLayer(boundaryLayer);boundaryLayer.refresh();
OS OpenSpace code examples
To get you started, here are some code examples to use within your own project.
- Displaying a marker on the map - pinpoint the surveyor's office.
- Displaying a marker on the map via coordinate translation - pinpoint the surveyor's location by converting latitude and longitude coordinates to British National Grid coordinates.
- Adding HTML to the marker info window - displaying HTML in the marker info window.
- Displaying a line overlay - the route between the surveyor's office and job.
- Displaying a polygon overlay - the surveyor's patch.
- Changing the marker icon - displaying a custom marker icon on the map.
- Displaying the position of the cursor - getting the coordinates of the cursor's position on the map.
- Displaying the map pan zoom control in a different position - putting the pan zoom control on the top right.
- Displaying the small map pan zoom control - putting the small pan zoom control at the top left.
- Using different map options - restricting layers and map area.
- Postcode search - centering a postcode on a map.
- Adding a boundary layer - displaying boundary attributes.
- Selecting boundaries with a hover control - displaying OS OpenSpace boundary attribution.
Technical FAQs
The following section covers a range of advanced information and technical FAQs.
- What is OpenLayers?
- What version of OpenLayers is used?
- OS OpenSpace API versioning
- How to include the new API version 0.8.0?
- How to include the OS OpenSpace API
- JavaScript namespaces
- How to report a bug
- Debugging
- How can I find the location of a place?
- Can I find the location of a postcode?
- Gazetteer search
- Can I use National Grid References?
- Creating markers and InfoWindow popup custom size
- How to detect daily tile quota exceeded
- How to turn off map centring when markers are clicked
- Downloading custom user data
- Introduction to Ordnance Survey data derived from the Boundary-Line™ product
- What is a boundary feature?
- Which Ordnance Survey data derived from the Boundary-Line™ product is available?
- Are there high level administration relationships in the Ordnance Survey data derived from the Boundary-Line™ products?
- Is there a lookup table of all the boundary features?
- What is the meaning of Ordnance Survey data derived from the Boundary-Line™ product RESOLUTION?
- Is the technology used to build OS OpenSpace available for me to host my own application?
- What is OSGB Web Map tools?
- Are there any restrictions using OSGB Web Map Tools?
Documentation
We have made all of the OS OpenSpace API documentation available to you.
Current Version
Previous Versions
Updates and changes
OS OpenSpace version 0.8.0
API Changes:
- Updated OpenLayers to version 2.7.
- Added support for postcode searches.
- Added support for requesting and displaying boundary information as user styleable vector polygons.
- Updated support service to return usage figures for gazetteer, postcode and boundary requests.
- Added Powered by OS OpenSpace logo and updated copyright message.
Show previous updates and changes
OS OpenSpace version 0.7.2
API Fixes:
- Fixed Info window text appearing blurred in Internet Explorer.
- Controls now move when map is resized.
- Fix vector layer to flicker less when panning
- Fix gazetteer search to return farms after towns and villages.
- Fix Screen Overlay to do relative positioning correctly.
- Fix to stop sending the wrong event when the map is being dragged.
- Fix CSS for copyright layer.
OS OpenSpace version 0.6
API Changes:
- There is a new method OpenSpace.SupportService.getTileCount() that an OS OpenSpace developer can call to get information on the number of map tiles downloaded and the tile limit.
API Fixes:
- Added methods to use or return OpenSpace.MapPoint objects from functions:
- OpenSpace.Layer.WMS.getMapPointFromViewPortPx()
- OpenSpace.Map.getCenter()
- OpenSpace.Map.setCenter()
- OpenSpace.Map.getMapPointFromViewPortPx()
- OpenSpace.Map.getViewPortPxFromMapPoint()
- OpenSpace.Map.getMapPointFromPixel()
- OpenSpace.Map.getPixelFromMapPoint()
- OpenSpace.Map.getMapPointFromLayerPx()
- OpenSpace.Map.getLayerPxFromMapPoint()
- Added missing OpenSpace.Icon.clone() method.
- Added missing SRS to OpenSpace.MapPoint.clone() method.
![OS OpenSpace [logo]](/images/openspace-logo.jpg)