4.2. Displaying a Scale Bar

Another typical widget to display on maps is a scale bar. OpenLayers provides an OpenLayers.Control.ScaleLine for just this. We’ll continue building on the previous example by adding a scale bar.

4.2.1. Creating a ScaleLine Control

Tasks

  1. Open the map.html produced in the previous example in your text editor.

  2. Somewhere in the map initialization (below the map constructor), add the following code to create a new scale line control for your map:

    var scaleline = new OpenLayers.Control.ScaleLine();
    map.addControl(scaleline);
    
  3. Save your changes and open map.html in your browser: http://localhost:8082/ol_workshop/map.html

    ../_images/scaleline1.png

    A default (and very hard to see) scale bar in the bottom left-hand corner

4.2.2. Moving the ScaleLine Control

You may find the scale bar a bit hard to read over the Medford imagery. There are a few approaches to take in order to improve scale visibility. If you want to keep the control inside the map viewport, you can add some style declarations within the CSS of your document. To test this out, you can include a background color, padding and a small transparency to the scale bar with something like the following:

.olControlScaleLine {
    background: white;
    padding: 5px;

    /* IE 8 */
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=85)";

    /* Netscape */
    -moz-opacity: 0.85;

    /* Good browsers */
    opacity: 0.85;
}

However, for the sake of this exercise, let’s say you think the map viewport is getting unbearably crowded. To avoid such over-crowding, you can display the scale in a different location. To accomplish this, we need to first create an additional element in our markup and then tell the scale control to render itself within this new element.

Tasks

  1. Remove any scale style declarations, and create a new block level element in the <body> of your page. To make this element easy to refer to, we’ll give it an id attribute. Insert the following markup somewhere in the <body> of your map.html page. (Placing the scale element right after the map viewport element <div id="map-id"></div> makes sense.):

    <div id="scaleline-id"></div>
    
  2. Now modify the code creating the scale control so that it refers to the scaleline-id element:

    var scaleline = new OpenLayers.Control.ScaleLine({
        div: document.getElementById("scaleline-id")
    });
    
  3. Save your changes and open map.html in your browser: http://localhost:8082/ol_workshop/map.html

  4. You may decide that you want to add a bit of style to the widget. To do this, we can use the #scaleline-id selector in our CSS. Make the font smaller and give the widget some margin, by adding the following style declarations:

    #scaleline-id {
        margin: 10px;
        font-size: xx-small;
    }
    
  5. Now save your changes and view map.html again in your browser: http://localhost:8082/ol_workshop/map.html

    ../_images/scaleline2.png

    A custom styled scale bar outside the map viewport.