Displaying a custom marker on a map

How do we display a custom marker on a map as above? 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(){osMap = new OpenSpace.Map('map');osMap.setCenter(new OpenSpace.MapPoint(439300, 114760), 8);var markers = new OpenLayers.Layer.Markers("Markers");osMap.addLayer(markers);// Default iconvar pos = new OpenSpace.MapPoint(438760, 114760);var marker = new OpenLayers.Marker(pos);markers.addMarker(marker);// Different iconvar size = new OpenLayers.Size(50, 50);var offset = new OpenLayers.Pixel(-size.w / 2, -size.h / 2);icon = new OpenLayers.Icon('house.gif', size, offset);pos = new OpenSpace.MapPoint(439870, 115048);marker = new OpenLayers.Marker(pos, icon);markers.addMarker(marker);}</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 somewhere between the two points we are about to define:
osMap.setCenter(new OpenSpace.MapPoint(439300, 114760), 3);
We create a 'markers' layer and add it to the map:
var markers = new OpenLayers.Layer.Markers("Markers");osMap.addLayer(markers);
Next we create a position object holding the location of Ordnance Survey head office:
var pos = new OpenSpace.MapPoint(438760, 114760);
Create a default marker at the above location:
var marker = new OpenLayers.Marker(pos);
Add the marker to the markers layer:
markers.addMarker(marker);
Create an icon containing the custom image we want to show on the map. The icon has its size specified and the offset the image needs to be shifted to appear over the chosen point:
var size = new OpenLayers.Size(50, 50);var offset = new OpenLayers.Pixel(-size.w / 2, -size.h / 2);icon = new OpenLayers.Icon('house.gif', size, offset);
The second marker is at a different position:
pos = new OpenSpace.MapPoint(439870, 115048);
Create the second marker:
marker = new OpenLayers.Marker(pos, icon);
Finally add the second marker to the markers layer, and close both the function and the script tag.
markers.addMarker(marker);}</script>
The full example can be found below.
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Open Space Tutorial - Example 6</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(){osMap = new OpenSpace.Map('map');osMap.setCenter(new OpenSpace.MapPoint(439300, 114760), 8);var markers = new OpenLayers.Layer.Markers("Markers");osMap.addLayer(markers);// Default iconvar pos = new OpenSpace.MapPoint(438760, 114760);var marker = new OpenLayers.Marker(pos);markers.addMarker(marker);// Different iconvar size = new OpenLayers.Size(50, 50);var offset = new OpenLayers.Pixel(-size.w / 2, -size.h / 2);icon = new OpenLayers.Icon('house.gif', size, offset);pos = new OpenSpace.MapPoint(439870, 115048);marker = new OpenLayers.Marker(pos, icon);markers.addMarker(marker);}</script><h1>Displaying a custom marker 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)