Ensuring scale bars and measurements are accurate on projected OpenLayers maps

Make sure you set geodesic: true when adding a ScaleLine or Measure control to an OpenLayers map that uses a projected coordinate system - particularly spherical mercator, which is very inaccurate at our latitudes

By default, the OpenLayers ScaleLine and Measure controls will be inaccurate on projected maps. Below, I’ve used a measuring tool on a google projection map to measure the 200m scale line (you can just about see the orange line I drew) and it comes out as 127m


When I first added the measuring tool, I calibrated it by measuring the 1km edge of the visible grid square and I got a surprising result. 1600m – nearly a mile. It turns out that when you use a projected coordinate system for your map in OpenLayers you must set the geodesic property of the measure control to true to get accurate measurements. By default it is false, which is ‘correct’ for geographic coordinate systems. When I did this for the Measure control I got the right distances with it.

It turns out the same applies to the ScaleLine control. Set geodesic to true in the constructor like this: new OpenLayers.Control.ScaleLine({ geodesic: true })

Here are the results. Note the scale bar  looks correct and the measurement I took of it is also (roughly) right.



Find out more