I am new with OpenLayers 3. I'm working with the projection EPSG:4326.

I can get current resolution from view.getResolution(), but I am having trouble to get current scale of map. Please, give me a solution as soon as possible.


Below is a function to get the resolution from scale, you should be able to transform the other way round using the same logic:

  getResolutionForScale(scale, units) {
    var dpi = 25.4 / 0.28;
    var mpu = ol.proj.METERS_PER_UNIT[units];
    var inchesPerMeter = 39.37;
    return parseFloat(scale) / (mpu * inchesPerMeter * dpi);
| improve this answer | |
  • 1
    You assume that the DPI is 25.4 / 0.28 ... Why? Wouldn't the DPI change when you use the GIS app on a mobile device, for example? I am sorry if my questions sound silly, but I am still not very used to DPI calculations. – joaorodr84 Aug 30 '16 at 18:39
  • Where are the magic numbers (25.4, 0.28) coming from? :-) It would be nice to know! Thanks! – Benedek Simo Feb 11 at 19:26
  • 0.28 is defined in the OGC WMS specification as mm per pixel. 25.4 is just to get convert from inch to mm – bartvde Feb 13 at 7:34
  • Thanks! @bartvde – Benedek Simo Mar 1 at 9:38

I think scale is not that useful for screen devices, you can obtain the scale by getting the resolution and multiplying it for the amount of pixels in your desired unit. The first answer to this question, Getting the physical screen dimensions / dpi / pixel density in Chrome on Android , explains with great detail how reference pixels correspond to real units, so 96 pixels should be equal to an inch and your scale would be 1:(resolution*96). I think a better approach is to draw a scale line as most sites do, and OpenLayers 3 has ol.control.ScaleLine for that.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.