I want to zoom to ol3 map base on scale(for example 1:50000). This means every 1cm in ol map must show 50000cm in real world. for converting scale to resolution I used as follows:

var getResolutionFromScale = function(scale, units) {
    var dpi = getDpi();
    var mpu = ol.proj.METERS_PER_UNIT[units];
    var inchesPerMeter = 39.37;
    return parseFloat(scale) / (mpu * inchesPerMeter * dpi);

function getDPI()
    var div = document.createElement("div");
    var body = document.getElementsByTagName("body")[0];
    var dpi = document.defaultView.getComputedStyle(div, null).getPropertyValue('width');
    return parseFloat(dpi);

Now for testing it, I use ScaleLine and when scaleLine controle show 1000m, it's length must be 2cm, but it is almost 2.5cm.

Where is th problem? How can I zoom base on scale?

Online Demo

  • check if the value you get for the dpi is even close to what you expect – Ian Turton Jun 19 '18 at 13:08
  • If your map is in Web Mercator EPSG:3857 the scale is changing by the latitude. Compare Greenland and Africa openstreetmap.org/#map=1/18/138. – user30184 Jun 19 '18 at 13:36
  • @IanTurton In any web sites that calculate dpi, theires return 96 for me. – Morteza Malvandi Jun 20 '18 at 8:33
  • @IanTurton I added an online demo – Morteza Malvandi Jun 20 '18 at 13:35
  • @user30184 I added an online demo – Morteza Malvandi Jun 20 '18 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.