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.

  1. It isn't possible to pan to the far North of the island.
  2. The map isn't centred in the <div> tag when it is first loaded.
  3. The zoom bar has no indication of the number of zoom levels, or how far zoomed in the view is.
  4. The zoom bar has a world icon, which may not be appropriate.
  5. Page loading is paused while the map is drawn, with the remainder of the page being displayed after the map.

Contents | Next

Creative Commons License This work is licensed under a Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License.


Russ' OpenStreetMap pages