Using OpenLayers with an Image File
Making an Initial Map
The following code displays a simple map:
<!-- div to display map -->
<div id = "map" class = "map"></div>
<!-- Load OpenLayers JavaScript library -->
<script src = "OpenLayers.js" type = "text/javascript"></script>
<!-- JavaScript to create & show the map -->
<script type = "text/javascript">
//Create map variable - this is the entire map, which can have multiple layers
var map = new OpenLayers.Map ("map")
//Create bounds & size objects. These are part of the definition of the image layer
//The simplest option is to set the first two parameters for bounds to 0,
//and the second two to the width & height of the image in pixels
bounds = new OpenLayers.Bounds (0, 0, 416, 496)
//Simply set the size option to the width & height of the image in pixels
size = new OpenLayers.Size (416, 496)
//Create the image layer
var img = new OpenLayers.Layer.Image ("Isle of Man", "map.png", bounds, size)
//Add the image layer to the map
map.addLayer (img)
//Set the initial zoom level
map.zoomToMaxExtent ()
</script>
The Map
The output of the above code is shown below:
We now have a map that can zoom and pan, but it's not ideal.
- It isn't possible to pan to the far North of the island.
- The map isn't centred in the <div> tag when it is first loaded.
- The zoom bar has no indication of the number of zoom levels, or how far zoomed in the view is.
- The zoom bar has a world icon, which may not be appropriate.
- Page loading is paused while the map is drawn, with the remainder of the page being displayed after the map.
Contents
| Next
This work is licensed under a Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License.
Russ' OpenStreetMap pages